Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the default random: argument in Ruby's Array#shuffle

The documentation for Array#shuffle states:

shuffle(random: rng) → new_ary

The optional rng argument will be used as the random number generator.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a.shuffle(random: Random.new(1))  #=> [1, 3, 2]

If I don’t supply the optional random argument, what is used for it?

Equivalently, if I call a.shuffle(random: rng), what does rng need to be to make it the same as just a.shuffle?

>Solution :

It’s right in the signature, shuffle(random: Random). That says the default value for random is the Random Class object.

The class method Random.rand provides the base functionality of Kernel.rand along with better handling of floating point values. These are both interfaces to the Ruby system PRNG.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading