4

Possible Duplicates:
Understanding “randomness”
Fastest implementation of a true random number generator in C#

Hello. Function:

Random rand = new Random();
rand.Next()

It gives pseudo random numbers based on time in seconds. How to get really random numbers? I mean totally random not based on system time and some algorithm. Like, when you drop a cube you got really random number, not based on time and any algorithm.

What do you think about using short sample from mic or time between mouse position change?

Community
  • 1
  • 1
Hooch
  • 28,817
  • 29
  • 102
  • 161
  • Was just discussed yesterday: http://stackoverflow.com/questions/3956478/understanding-randomness –  Oct 19 '10 at 12:41
  • 2
    Google for cosmic background radiation, then grab a sample. – leppie Oct 19 '10 at 12:41
  • What language is this? (apologies for ignorance, assuming C#?) – richsage Oct 19 '10 at 12:41
  • 4
    You have to observe a physical process to obtain a really random number. – Peter G. Oct 19 '10 at 12:41
  • You might be interested in this question: http://stackoverflow.com/questions/3956478/understanding-randomness – Trufa Oct 19 '10 at 12:41
  • 2
    Hook up a dice throwing device and a camera with your machine. Every time the die gets thrown, use image recognition to read in the value. Is that random enough? – MAK Oct 19 '10 at 12:44
  • @leppie-I think numerical recipes in c includes a CD with random solar noise run through a compression algorithm for "extra randomness" that's supposed to be pretty good too. – Marc Oct 19 '10 at 12:47
  • @leppie Once you have a finite set it is no longer random since there is a repeating pattern. A repeating pattern is, by definition, not random. –  Oct 19 '10 at 18:32

9 Answers9

26

Put a cat in a box with a bottle of poison, which will be released when a single atom of a radioactive material decays. Come back in one hour and open the box. If the cat is dead, use 0 as a random bit in your program. If it is alive, use 1.

Repeat with as many cats and boxes as is necessary.

AndrewF
  • 6,852
  • 7
  • 29
  • 27
7

you could use a service such as random.org

jk.
  • 13,817
  • 5
  • 37
  • 50
5

Ask the user to hit the spacebar a few times. Use the time in microseconds between keystrokes to seed your random number generator.

(or better, use the remainder of the time in microseconds mod 256 microseconds to eliminate any human periodicity. So each keypress gives you 1 random byte, use the appropriate number of strokes to seed your generator)

Marc
  • 5,315
  • 5
  • 30
  • 36
  • @Anders: Please don't use tinyurl in SO. – MAK Oct 19 '10 at 12:58
  • @MAK, Ok. http://books.google.com/books?id=-kcKpqzbBR0C&pg=PA254&lpg=PA254&dq=GUI+Bloopers+%2B+Random+number&source=bl&ots=rPcMkV2ZZR&sig=nVfoIfb4AqVw2xwpQdmcCo3q9zA&hl=sv&ei=W5S9TO2eGcbGswbsk4DEDQ&sa=X&oi=book_result&ct=result&resnum=3&ved=0CCQQ6AEwAg#v=onepage&q&f=false , Don't involve the user in this process, it's bad practice. – Anders Oct 19 '10 at 12:59
  • I wiggle a mouse to generate an RSA key and it doesn't really bother me. I guess it depends on how many numbers and how often. Humans are actually really good at generating randomness because we can't repeat anything precisely. This is why people still polish telescope mirrors by hand. Anyway, given that OP provides no information on his system, I'm not sure where else you get randomness from, generically. – Marc Oct 19 '10 at 13:23
  • 1
    To address the specific point in your book excerpt, obviously you shouldn't ask the user to input a random seed, which is not what I'm suggesting here. But if it makes you happy, unless the application you are writing doesn't take any user input at all, use the time between keystrokes, mouse wiggles, etc. in other parts of the UI to initialize the RNG, rather than specifically asking for a number of key presses. – Marc Oct 19 '10 at 13:31
5

Most programming random functions are based on mathematical algorithms. There are random numbers generators, that use wave noises in air (from space radiation), but they're hardware based.

mlusiak
  • 1,054
  • 14
  • 28
5

For that, you would need a hardware random number generater.

A good-enough-for-most-cases substitute for this is measuring the timing of "hardware events" such as user input or network activity.

On Linux, the /dev/random device produces bytes based on this method.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
4

I would recommend you:

True Random Number Generators (TRNGs)

http://www.random.org/randomness/

That will point you in the right direction!

Trufa
  • 39,971
  • 43
  • 126
  • 190
4

It's very difficult to get really random numbers. PRNGs are, after all, in some sense predictable. But it's much more difficult to correctly build a random number generator based on a physical source; most random sources don't produce real random bits all that fast, and it's hard to tell whether you've made a bad blunder. A good PRNG is better for virtually all intents and purposes.

(Only use a high-quality PRNG if you need one, as they're much slower, and make sure you use the right kind if you do: a PRNG designed for crypto is not the same as one designed for Monte Carlo simulation – crypto PRNGs make sure every bit is hard to guess, whereas MCSim PRNGs make sure that spectral properties are good – and most other uses for PRNGs just want them to not be cruddy and to be fast; the standard library one is usually good enough there.)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
3

http://www.entropykey.co.uk/

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
2

You could use the System.Security.Cryptography.RNGCryptoServiceProvider.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125