DEV Community

Cover image for Rando.js: replacing Math.random()

Rando.js: replacing Math.random()

nastyox on March 08, 2020

πŸ™‰ What's all the hullabaloo? There's now a vastly better alternative to JavaScript's built-in Math.random() that will make your life ea...
Collapse
 
mich_cook profile image
mich

This looks quite useful. Given that Math.random() returns a float, what drove your decision to make int the default return type in the numerical instances such as rando(5) and rando(5,10) instead of float? Was it an assumption that most people specifying numbers were looking for ints or something else?

Collapse
 
nastyox1 profile image
nastyox

Thanks! That's a great thought. And yes, that's exactly right. I've seen ints as a much more common use case than floats (excluding [0-1)), so I decided to make that the easier of the two. If you have other thoughts on the project, I'm always interested to hear feedback. You can leave a comment here, DM me on here, or create a pull request on my GitHub.

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

Hot tip, include language after the leading backticks for codeblocks to get food syntax hilighting.

example

result

randoSequence(5)  
   //a random integer between 5 and 10 (could be 5 or 10)  

   randoSequence(5, 10)  
   // an array of integers from 5 through 10 in random order  

   randoSequence(["eeny", "meeny", "miny", "moe"])  
   // array of {index:..., value:...} objects representing the values of the provided array in random order  

   randoSequence({species: "mouse", blind: true, count: 3})  
   // array of {key:..., value:...} objects representing the properties of the provided object in random order  

   randoSequence($("input"))  
   // array of {index:..., value:...} objects representing a jQuery elements from the provided jQuery element set in random order  

   randoSequence("Good gravy!")  
   // the characters of the provided string in random order  

   randoSequence(null)  
   // ANY invalid arguments return false

EDIT 🀣 corrected hot too to hot tip

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

food syntax 🌭

Unexpected hotdog.
Collapse
 
nastyox1 profile image
nastyox

Nice tip. Thanks :)

Collapse
 
po5i profile image
Carlos V.

Looks promising! Is this library a pseudorandom one like Math.random or is it a true random algorithm?

Collapse
 
mrspartak profile image
Spartak

It is using Math.random, just added sugar around it.
Made a ton prototypes long time ago for such tasks

Array.prototype.getRandom = function () {
    return this[Math.floor(Math.random() * this.length)];
};
console.log( [1, 2, 3].getRandom() )
Collapse
 
krishnakakade profile image
krishna kakade

I added this in my reading list looks interesting πŸ±β€πŸβ€

Collapse
 
daxsoft profile image
Michael Willian Santos

Amazing! I'll study in how to implement it into my projects. Thank you

Collapse
 
nastyox1 profile image
nastyox

It's been getting some attention, so I upgraded it to a cryptographically secure random now. Thanks for being interested in it from the start.

Collapse
 
rekreanto profile image
rekreanto

Just love the design of the website <3. The combination of sparseness, clarity and slight aliveness/animation is awesome! randojs.com/

Collapse
 
nastyox1 profile image
nastyox

thanks a bunch!!

Collapse
 
andygithubchen profile image
andy chen

Cool !

Collapse
 
theharshsingh profile image
Harsh Singh

I had also created a tiny lib for generating random stuff ( integers, floats, hex colors, shuffling an array, unique IDs, etc. ) for personal use. its called kram.js

github.com/rwbeast/kram

Collapse
 
island_dev profile image
Joey The Dev

Good work! I might use this in a future project.

Collapse
 
calvinpak profile image
CP

Very nice work!

Collapse
 
cledilsonweb profile image
Cledilson Nascimento

Very good! randoSequence () is excellent! / Muito bom! randoSequence() Γ© excelente!

Collapse
 
javierx2010 profile image
Javier Escobar

I think I could use this sometime... Thanks!

Collapse
 
anshul_gupta profile image
Anshul Gupta

@nastyox
You literally get a domain name to host a website for this project πŸ˜‚.
I think you have better plans for Rando.js.

Collapse
 
charlygarcia120 profile image
Carlos Alberto

thanks!!!!

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

If you want an entire replacement of the math std library, I am a bit of a fanboy about this one. mathjs.org/

Collapse
 
louy2 profile image
Yufan Lou