How to get real random numbers in ruby1.9.3?

The question is in the title. Is there any stone for this? Tried RealRand, but it seems that this does not work for me. Maybe I'm doing something wrong?

+4
source share
4 answers

Ruby 1.9 introduces a class called Random , which generates pseudo-random numbers based on the Mersenne Twister algorithm. For practical use on a personal level, this should be enough.

+2
source
Peter is right. However, we can get pretty good randomness with openssl and safe random .

code:

  require "securerandom" puts SecureRandom.random_number 
+2
source

The best definition and source of β€œreal” random numbers (which are not pseudo-random) I found at http://realrand.rubyforge.org/ In short, this means that β€œreal” random numbers cannot be genetically processed by computer and algorithm, it can only be generated by nature. I know theories that say nature is just a bunch of algorithms, so I think it's food for debate that is above my level. In any case, the site provides some sites that generate random numbers from things such as "temporary pairs of radioactive decays detected by a Geiger-Muller pipe connected to a computer."

I am interested in what comments and answers this generates, +1 from me.

+1
source

I think you may have to think a little about your question. There are more real numbers between 0 and 1, then there are integers between negative infinity and infinity. Give a wikipedia article on power on real numbers look .

But you can easily generate real random numbers within range and accuracy. Suppose you wanted to create a real random number from 0 to 1 with 16 digits of precision.

 r = Random.new r.rand => 0.7182182166496581 

You can adapt this solution to generate real numbers from -10 to 10.

  r.rand(-10.0..10.0) => 7.00152804654363 

Check out http://www.ruby-doc.org/core-1.9.3/Random.html and How to get a random number in Ruby for more information.

+1
source

Source: https://habr.com/ru/post/1441643/


All Articles