this post was submitted on 01 Oct 2024
56 points (92.4% liked)

Ask Lemmy

26996 readers
1492 users here now

A Fediverse community for open-ended, thought provoking questions

Please don't post about US Politics. If you need to do this, try !politicaldiscussion@lemmy.world


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
you are viewing a single comment's thread
view the rest of the comments
[–] SzethFriendOfNimi@lemmy.world 17 points 1 month ago* (last edited 1 month 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.