DEV Community

Cover image for 3 Amazing ways to generate random numbers without Math.random()

3 Amazing ways to generate random numbers without Math.random()

Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’» on October 04, 2019

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...
Collapse
 
wikunia profile image
Ole KrΓΆger

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

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

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

Collapse
 
wikunia profile image
Ole KrΓΆger • Edited

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.

Thread Thread
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

I think it is better to be explicit in this case.😎
Without knowing the target system, using multiplication is a risk.πŸ™

Thread Thread
 
wikunia profile image
Ole KrΓΆger • Edited

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.

Thread Thread
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

Oh... Okay πŸ‘πŸ½. You're right. Why I didn't do is because,I thought it would be confusing to the readers.

Thread Thread
 
wikunia profile image
Ole KrΓΆger

Fair enough. Maybe an extra box for extra information but understand your point.

Collapse
 
tomatoes_the profile image
TheTomatoes • Edited

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

Collapse
 
epse profile image
Stef Pletinck

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.

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

Thank you for pointing this outπŸ–’. I didn't know about the randomness modules in CPUs 😰

Thread Thread
 
epse profile image
Stef Pletinck

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.

Thread Thread
 
artis3n profile image
Ari Kalfus

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.

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

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. πŸ˜“

Collapse
 
netmailgopi profile image
Gopi Ravi

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.

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

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.

Collapse
 
5th profile image
Marius

Dude, like, It's literally what I need for my class project. I have to generate randomness from scratch πŸ˜‚πŸ‘Œ

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

I'm glad it helped πŸ˜„

Collapse
 
myterminal profile image
Mohammed Ismail Ansari

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?

Collapse
 
shalvah profile image
Shalvah • Edited

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)?
Collapse
 
kephas profile image
Nowhere Man

Why, ho why would you present PRNGs without a single warning about the use of cryptographically secure randomness where security is needed?

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

I didn't want to confuse you people with cryptography and cryptographically secure PRNGs.

Collapse
 
amatiasq profile image
A. MatΓ­as Quezada

Middle square method without string manipulation:

function myRandom() {
  seed = Math.floor(((seed ** 2) % 1000) / 10);
  return seed;
}
Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

Wow! Great work πŸ‘πŸ‘πŸ‘πŸ‘! Why didn't I think of this? 😁

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

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 πŸ™‚

Collapse
 
agsb profile image
Alvaro Gomes Sobral Barcellos

For no encryption uses, do not forgot tha old, easy, fast, venerable, 997 multiplier algorithm.

Collapse
 
tkelogue profile image
KT

Thanks Very helpfull !

Collapse
 
svijaykoushik profile image
Vijay Koushik, S. πŸ‘¨πŸ½β€πŸ’»

Sure! πŸ˜‚