4

The question is in header. Is there any gem to do this? Tried RealRand but seems it doesn't work for me. Maybe I'm doing something wrong?

4
  • Operative system? if you can, read from: /dev/random
    – yeyo
    Oct 23, 2012 at 20:36
  • Can you give a range? There is uncountably many infinite numbers between 0 and 1. Oct 23, 2012 at 20:43
  • 1
    What have you tried? Without a sample of the code you've written we're just shooting in the dark about what you could be doing wrong. Or, is it you haven't written anything and you're looking for someone to do it for you? Oct 23, 2012 at 21:09
  • 3
    I think the people answering this are taking your word "real" in two different meanings. (a) peter, me and VP are taking it as opposed to pseudo. (b) Sunny Jenuja is taking it as opposed to for e.g., rational, complex, etc.
    – sawa
    Oct 23, 2012 at 21:13

4 Answers 4

2

Ruby 1.9 introduces a class called Random, which generates pseudo random numbers based on an algorithm called Mersenne Twister. For practical use at personal level, that should probably suffice.

1
  • -1 This is not at all what the OP asked for? I understand that a pseudo-random generator is (T,e)-indistinguishable from a random generator, but ignoring the question without explanation seems unreasonable.
    – Ziggy
    Feb 25, 2015 at 18:44
2

Peter is right. However we can get pretty much good randomness with openssl and the secure random.

code:

 require "securerandom"
 puts SecureRandom.random_number
1

The best definition and source of "real" random numbers (that are not pseudorandom) i found at http://realrand.rubyforge.org/ In short it stipulates that "real" random numbers can't be genereated by a computer and an algorithm, it can only be generated by nature. I know of theories that say that nature is just a bunch of algorithms so i figure that is food for debates that are beyond my level. In any case, the site gives some sites that generate random numbers from things like "timing successive pairs of radioactive decays detected by a Geiger-Müller tube interfaced to a computer".

I'm interested in what comments and answers this generates, a +1 from me.

1

I think you may need to give a bit more thought to your question. There are more real numbers between 0 and 1 then there are integers between negative infinity and infinity. Give the wikipedia article on the cardinality on real numbers a look.

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

r = Random.new
r.rand
=> 0.7182182166496581 

You can adapt this solution to generate real numbers between -10 and 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 info.

3
  • 1
    I think you may need to give a bit more thought to your answer. In digital computing, numbers are expressed as float, with combination of finite binary digits. Real numbers cannot be handled in the first place. The OP likely have not meant "real" in the sense you take. Your code does not give random numbers with correct distribution over real numbers ("real" taken in your sense). By the way, I don't see why you mention the cardinality, but the cardinality of the closed interval [0, 1] is the same as the cardinality of all real numbers. Whether you specify a range makes no difference.
    – sawa
    Oct 23, 2012 at 23:26
  • I never claimed the cardinality was greater than on [0,1] than the entire real numbers or any subset of R. The reason I brought up cardinality is because like you said, it would not be possible to create a real random number from -infinity and infinity as I think the author desires. Although, I see your point that the code not provide true randomness over the entire distribution over R. I'll amend my answer to say that it provides real random numbers over a set precision. Oct 23, 2012 at 23:45
  • Can we even create irrational numbers at all? Wouldn't they require infinite storage?
    – Ziggy
    Feb 25, 2015 at 18:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.