this post was submitted on 01 Oct 2024
31 points (89.7% liked)

Ask Lemmy

26366 readers
1063 users here now

A Fediverse community for open-ended, thought provoking questions


Rules: (interactive)


1) Be nice and; have funDoxxing, trolling, sealioning, racism, and toxicity are not welcomed in AskLemmy. Remember what your mother said: if you can't say something nice, don't say anything at all. In addition, the site-wide Lemmy.world terms of service also apply here. Please familiarize yourself with them


2) All posts must end with a '?'This is sort of like Jeopardy. Please phrase all post titles in the form of a proper question ending with ?


3) No spamPlease do not flood the community with nonsense. Actual suspected spammers will be banned on site. No astroturfing.


4) NSFW is okay, within reasonJust remember to tag posts with either a content warning or a [NSFW] tag. Overtly sexual posts are not allowed, please direct them to either !asklemmyafterdark@lemmy.world or !asklemmynsfw@lemmynsfw.com. NSFW comments should be restricted to posts tagged [NSFW].


5) This is not a support community.
It is not a place for 'how do I?', type questions. If you have any questions regarding the site itself or would like to report a community, please direct them to Lemmy.world Support or email info@lemmy.world. For other questions check our partnered communities list, or use the search function.


Reminder: The terms of service apply here too.

Partnered Communities:

Tech Support

No Stupid Questions

You Should Know

Reddit

Jokes

Ask Ouija


Logo design credit goes to: tubbadu


founded 1 year ago
MODERATORS
all 24 comments
sorted by: hot top controversial new old
[–] maniel@sopuli.xyz 12 points 5 hours ago (1 children)
[–] cheese_greater@lemmy.world 2 points 5 hours ago

All random, all thuh time

[–] SzethFriendOfNimi@lemmy.world 8 points 5 hours ago* (last edited 5 hours ago)

I see a lot of good answers here but let’s try it from another angle.

How do we get randomness from a function or formula?

For starters let’s setup a few simple rules.

Every time our random function is called we’ll

  • Take the last output from a variable we call LAST_RESULT
  • If there’s no value in LAST_RESULT we’ll assume the value is 1
  • We run a set of calculations storing the value in a variable we call X
  • We store the result of these calculations in LAST_RESULT
  • We return this new “random” number.

So let’s call it.

> Random()
Since LAST_RESULT is undefined SET LAST_RESULT to the value of 1
Set X to the result of this calculation 
   (LAST_RESULT+1) * 3

X is now 6

Set X to the result of this calculation
   (X + 7) / 2

X is now 7

Set X to the result of this calculation (rounding to the nearest whole number)
   X/LAST_RESULT

X is now 7

Set LAST_RESULT to the value of X

LAST_RESULT is now 7

Return the value of X as the result 

Result is 7

Ok. So let’s call it again

 > Random()
Set X to the result of this calculation 
   (LAST_RESULT+1) * 3

X is now 24

Set X to the result of this calculation
   (X + 7) / 2

X is now 16

Set X to the result of this calculation (rounding to the nearest whole number)
   X/LAST_RESULT

X is now 2

Set LAST_RESULT to the value of X

LAST_RESULT is now 2

Return the value of X as the result

Result is 2

And if we call it again we get seemingly random results

Random() Result is 4

Random() Result is 3

But the next time you run it you’ll get the same results in the same order. 7, then 2 then 4 then 3

So what you need is something to “seed” the random number calculation.

Something like

SetRandomSeed Set LAST_RESULT to the current second of the day

Then when you call Random after this it starts with that as the prior results and gives seemingly random results.

Of course my calculations are rough and probably fail/repeat after so many calls but it gives you an idea of how this works.

So the trick is to get noise for the seed. That could be the number of non leap seconds since 00:00:00 UTC on Thursday, 1 January 1970 (Unix epoch)

Or the temperature reading of a CPU chip.

Maybe it’s the ratio of red vs yellow from a camera feed looking at lava lamps.

Or the current users average typing speed.

An additional note. Many of those would not be “cryptographically” secure for encryption because they can easily be determined by a third party. We all experience the same “Unix epoch” within a few milliseconds if our system clocks are properly set for example. Or monitored from afar and reproduced (hacked webcam shows they had just typed the following letters in the previous 27 seconds that we know the “algorithm” uses, etc.

[–] paw@feddit.org 7 points 6 hours ago* (last edited 6 hours ago) (4 children)

From my opinion it is more computer science sorcery than math sorcery.

For true random generation you usually need some specialized hardware for it, that uses sone natural source of random. One could use the decay of a radioactive material as such a source or the noise one can get from audio input. Unfortunately, I don't know what actual hardware uses.

For pseudo random generation, you usually use a seed (ideally a true random value or something with a high entropy) which you feed into an algorithm like Linear Congruental Generator (LCG) or Mersenne Twister (there are lots of algorithms).

One further important note: Tge use case forvwhich you need random numbers is important. A video game could accept a random number generator with "lower" quality while a cryptographic algorithm always needs a cryptographic secure random number generator (don't forget: "don't roll your own crypto").

Finally there are quasi randim number generators, however this name is very misleading. The mathematical correct term is low discrepancy sequence. There are not random at all but can be used and have useful properties in some settungs where pseudo random number generators can be used. Never in a cryptographic algorithm, though.

[–] paw@feddit.org 1 points 5 hours ago* (last edited 5 hours ago)

Another tidbit: Operating systems (like Linux) usually provide a possibility to get entropy (ideally used as seed). Linux for example has /dev/urandom beyond others. Afaik, it uses the time between subsequent accesses to the hdd as one of the sources used to create the entropy.

[–] Treczoks@lemmy.world 3 points 5 hours ago

An interesting source of randomness is using a diode "in reverse". Randomly, a few electrons pass through, which can be amplified and measured. One uses a 2^n number of such constructs and XORs the results to get a random bit.

[–] bulwark@lemmy.world 2 points 6 hours ago (1 children)

Great write up, now I have to google what a Meraenne Twister is. To use audio input noise as a random number gen I would just hook it up to a pressision digital db meter but I'm guessing the software implementation is a little more practical.

[–] paw@feddit.org 2 points 5 hours ago* (last edited 5 hours ago)

A software solution usually can create "random" faster, with the drawback that its not actual random

The Mersenne Twister was a famous pseudo random number generator when I wrote my diploma thesis in 2009. Today, afaik, PCG (Permuted Congrentual Generator) are better.

[–] Zak@lemmy.world 4 points 5 hours ago* (last edited 5 hours ago)

PRNGs aren't random at all; they produce a deterministic sequence of numbers based on a seed value and an internal counter. Two PRNGs using the same algorithm and seed will produce the same sequence of numbers. The sequence is difficult to predict without knowing the algorithm and seed, and the values are close to evenly-distributed, which is enough like random numbers for a lot of use cases.

Here's an example in Ruby:

seed = Random.new_seed()
=> 142757148148443078663499575299582907518
prng_1 = Random.new(seed=seed)
prng_1.rand()
=> 0.6702742156250219
prng_2 = Random.new(seed=seed)
prng_2.rand()
=> 0.6702742156250219
prng_1.rand()
=> 0.9667236181962573
prng_2.rand()
=> 0.9667236181962573

If you run this yourself using 142757148148443078663499575299582907518 as the seed, your first two pseudorandom numbers will also be 0.6702742156250219 and 0.9667236181962573, assuming your version of Ruby hasn't changed its PRNG.

[–] NOT_RICK@lemmy.world 37 points 6 hours ago (1 children)
[–] owenfromcanada@lemmy.world 27 points 6 hours ago

Math! Also, noise!

There are algorithms (a set of math steps) that make pseudo-random numbers. These usually involve large prime numbers, because those usually generate fewer repeating patterns.

A truly random number generator is similar to rolling dice: you use some source of randomness and convert it to a number. All electric circuits produce "noise" (which is often received radio waves and such that interfere with the circuits). Think of tuning a radio to a channel with nothing on it--you get "white noise", which can be a good source of random information. Then all you need to do is convert that to a range of numbers, and you're good to go.

These are fairly simplified explanations, so take them with a grain of salt, but they give the general idea.

[–] HottieAutie@lemmy.dbzer0.com 8 points 6 hours ago (1 children)

They get some dude hammered drunk, then stand him by a pole and ask him to take 100 steps.

[–] whereBeWaldo@lemmy.dbzer0.com 3 points 5 hours ago

I love the idea of millions of drunk dudes by poles being employed just for random number generation

[–] yesman@lemmy.world 4 points 5 hours ago

You can use physical objects like dice or lava lamps that will naturally form random distribution when we check. But Newton and others would argue that even this was a determinant problem and if you had perfect knowledge of the dice and a good physics theory, you could predict the outcome.

We can only recognize randomness by the patterns it leaves behind.

The philosophical truth is that we don't know if "randomness" is an actual phenomena or just a bucket where we put outcomes we haven't learned to predict yet. A sort of randomness of the gap. Some have suggested that as a pattern-recognizing machine, the human mind simply can't conceive randomness. Even the way "randomness" is verified is by looking at the distribution in the outcome and see if it matches the pattern we expect.

[–] Callypo@lemmy.world 3 points 6 hours ago (1 children)
[–] cheese_greater@lemmy.world 1 points 6 hours ago

1000111000111100001100101010101001111111010010

[–] Diplomjodler3@lemmy.world 3 points 6 hours ago

All you need is a bunch of lava lamps and a camera.