Trying to learn as much as possible and give it back to the community using my blog: opensourc.es where "es" is more the german "it" than the spanish tld. So go out and opensource your projects!
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 than 42<<5
Trying to learn as much as possible and give it back to the community using my blog: opensourc.es where "es" is more the german "it" than the spanish tld. So go out and opensource your projects!
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.
Trying to learn as much as possible and give it back to the community using my blog: opensourc.es where "es" is more the german "it" than the spanish tld. So go out and opensource your projects!
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.
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.
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.
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:
the title kinda makes it sound like you're suggesting alternatives to Math.random() (which is why you have so many comments saying people should use the library😆), rather than just exploring how PRNGs are implemented
could you explain how these algorithms get their starting seed? If you used the same seed (and modulus, multiplier, increment) always, it's no longer pseudo-random. Do they store the last used seed somewhere? Use the clock (which makes it sort of predictable)?
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 🙂
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<<5It 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.
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.
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?
Dude, like, It's literally what I need for my class project. I have to generate randomness from scratch 😂👌
I'm glad it helped 😄
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.
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 🙂
Middle square method without string manipulation:
Wow! Great work 👍👏👏👏! Why didn't I think of this? 😁
Thanks Very helpfull !
For no encryption uses, do not forgot tha old, easy, fast, venerable, 997 multiplier algorithm.
Sure! 😂