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
Β 
krishnakakade profile image
krishna kakade β€’

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

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
Β 
daxsoft profile image
Michael Willian Santos β€’

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

Collapse
Β 
andygithubchen profile image
andy chen β€’

Cool !

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
Β 
calvinpak profile image
CP β€’

Very nice work!

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
Β 
island_dev profile image
Joey The Dev β€’

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

Collapse
Β 
javierx2010 profile image
Javier Escobar β€’

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

Collapse
Β 
cledilsonweb profile image
Cledilson Nascimento β€’

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

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
Β 
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
Β 
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
Β 
louy2 profile image
Yufan Lou β€’