Have you ever played an online spin to win game? You know! The one that pops up as an Ad with the message βSpin this to win the latest, feature-ric...
For further actions, you may consider blocking this person and/or reporting abuse
I think it's nice to mention that bit shifting isn't just a bit operation which produces a completely different number but instead a multiplication or division by a power of two so 42*25 = 1344
Umm. No! computers(CPUs) do not handle bit shifts like you mentioned. Rather it takes place entirely in Binary in the CPU registers. The instruction
42*(2**5)
consumes more resources of the CPU than42<<5
It produces the same result that's all I said. Nevertheless in C++ it's compiled to the same (42*32) and 42 << 5 both to the latter because bit shifting is faster.
I think it is better to be explicit in this case.π
Without knowing the target system, using multiplication is a risk.π
I didn't want that it uses multiplication. I thought it would be nice to mention that the result (mathematically) is the same instead of: it's changing the result to make a completely new number. Make it clear to the reader what this does and how to visualize it. Then people who don't know it can use it if they want to multiply by a power of two somewhere.
Oh... Okay ππ½. You're right. Why I didn't do is because,I thought it would be confusing to the readers.
Fair enough. Maybe an extra box for extra information but understand your point.
Actually you don't need radioactive stuff; some computer use a very simple piece of hardware: the microphone. Sound (fan, conversation...) is indeed totally random and unpredictable
Modern cpus also tend to have randomness modules in them, that generally look at everything that is running and timing of interrupts, like key presses or WiFi packets.
Thank you for pointing this outπ. I didn't know about the randomness modules in CPUs π°
They are generally exposed through the OS/Kernel's randomness functions, which are then used again in other languages and programs. I don't know if Math.rand() uses it or not though.
I don't want to toot my own horn, but I did write an article a while back on pseudo-randomness vs randomness in OS systems: dev.to/artis3n/random-vs-pseudorandom. You both may be interested in it.
I wanted to convey the scale of how far we've gone to generate true random numbers βΊ. Using the microphone sounded a bit creepy to me. That's why I didn't include it. π
I like this blog, I like how you ELI5 PRNG concepts and even implement them. While all that's cool, I'm of the opinion that one shouldn't be generally replacing/implementing Math.Rand unless necessary. Abstraction is good, usually.
Thank you βΊ. You are right. You can't use the code directly in production.The goal of this post is to show how easy it is to implement the PRNGs and I've left the implementation details to the readers.
Dude, like, It's literally what I need for my class project. I have to generate randomness from scratch ππ
I'm glad it helped π
Implementing 'randomness' in my programs has been a simple challenge for me: I use the timestamp or to be specific, the count of milliseconds elapsed since EPOCH, which in JavaScript is as simple as
new Date().getTime()
. I know it's probably not the best methods out there and I didn't even bother to read more about it. However, with my 'cheap' method to generate random numbers, there's always a seed: the time at that exact moment.I liked all of the three methods explained in this post but have a question: like we mentioned that the previous random number forms a seed to generate the next one, I'm wondering (I couldn't find it in the post) for what the seed would be for the first random number in the sequence?
Great article! I love how you explained the algorithms simply. Two things:
Math.random()
(which is why you have so many comments saying people should use the libraryπ), rather than just exploring how PRNGs are implementedWhy, ho why would you present PRNGs without a single warning about the use of cryptographically secure randomness where security is needed?
I didn't want to confuse you people with cryptography and cryptographically secure PRNGs.
Middle square method without string manipulation:
Wow! Great work ππππ! Why didn't I think of this? π
Hi, my code is a basic implementation of the algorithms. It doesn't cover all the parameters required for a strong PRNG. I wanted to show how easy it is to implement these algorithms π
For no encryption uses, do not forgot tha old, easy, fast, venerable, 997 multiplier algorithm.
Thanks Very helpfull !
Sure! π