# Random Number

Show 80 post(s) from this thread on one page
Page 3 of 4 First 1234 Last
• 05-31-2004
Davros
>Take a look at this site:
http://www.lavarnd.org/

The LavaCan is Interesting. However, I recall seeing tiny devices which plug into the RS232 port and cost around \$20 from RS Components.
• 06-02-2004
pianorain
Quote:

Originally Posted by Thantos
As such there must not be a way to generate a truely random set of numbers :)

One of my math professors kept a book called "The List of Random Numbers". The entire book (some 400 pages) was a chart of random numbers. I guess it didn't occur to the writer that once the numbers were ordered, they were no longer random. Must have been pretty simple to write the book. I'm pretty sure most of the people that frequent this board could write a program to write a sequel for it in under 20 minutes.
• 06-02-2004
JasonD
The book's contents may be truly random numbers, like those obtained by throwing dice, rather than pseudo random numbers generated by a computer. Basically, one set can be reproduced exactly, the other can not. I would assume a book would use a set that can not. I think that they would still be classified as random numbers, as you can start anywhere in the book, and as long as you don't already have the list memorized, you cannot determine which number will appear next - just like the die.

And even if we live in a deterministic universe, it would be very difficult to set up the environment - the entire universe - exactly as before to achieve the same results. In fact, we would not even realize that we were repeating the experiment as we would all have the same thoughts and memories as the first time.
• 06-02-2004
anonytmouse
If you need a 'better' random number on windows check out CryptGenRandom().
Quote:

CryptGenRandom documentation

The data produced by this function is cryptographically random. It is far more random than the data generated by the typical random number generator such as the one shipped with your C compiler.

This function is often used to generate random initialization vectors and salt values.

Software random number generators work in fundamentally the same way. They start with a random number, known as the seed, and then use an algorithm to generate a pseudo-random sequence of bits based on it. The most difficult part of this process is to get a seed that is truly random. This is usually based on user input latency, or the jitter from one or more hardware components.

With Microsoft CSPs, CryptGenRandom uses the same random number generator used by other security components. This allows numerous processes to contribute to a system-wide seed. CryptoAPI stores an intermediate random seed with every user. To form the seed for the random number generator, a calling application supplies bits it might have—for instance, mouse or keyboard timing input—that are then added to both the stored seed and various system data and user data such as the process ID and thread ID, the system clock, the system time, the system counter, memory status, free disk clusters, the hashed user environment block. This result is SHA-1 hashed, and the output is used to seed an RC4 stream, which is then used as the random stream and used to update the stored seed. If an application has access to a good random source, it can fill the pbBuffer buffer with some random data before calling CryptGenRandom. The CSP then uses this data to further randomize its internal seed. It is acceptable to omit the step of initializing the pbBuffer buffer before calling CryptGenRandom.
• 06-14-2004
weirdbeardmt
More randomness
Hi, sorry if I'm asking a dumb question (having looked through the FAQ/boards etc), but here goes:

I need to generate a list of numbers from 1 (to a set number, for e.g. 25) in a random order. However, I can't have the same number twice, but I need the list ordering to be random.

ANy thoughts?
• 06-14-2004
Thantos
Have an array that will store the numbers, a variable that will tell you how many items are in the array, and a temporary variable that will hold the result of each random call.

Process would be:
1) call the random generator and put value into the temporary variable
2) Search the array (using the above mentioned variable to make sure you only look at values you've put in already).
3) If you didn't find a match, add in the temp and increase the size counter. Repeat until filled
• 06-14-2004
Prelude
Quote:

Originally Posted by Thantos
Have an array that will store the numbers, a variable that will tell you how many items are in the array, and a temporary variable that will hold the result of each random call.

Process would be:
1) call the random generator and put value into the temporary variable
2) Search the array (using the above mentioned variable to make sure you only look at values you've put in already).
3) If you didn't find a match, add in the temp and increase the size counter. Repeat until filled

Search an array for each new random number? Even not taking into account duplicate values, this is a terribly inefficient process. IMO it's much better to just fill an array with the range of values and permute it randomly:
Code:

```#include <algorithm> #include <cstdlib> #include <iostream> using namespace std; double random(); int main() {   int *vals;   int  n;   cout<<"Upper limit: ";   if (!(cin>> n)) {     cerr<<"Input error"<<endl;     exit(EXIT_FAILURE);   }   // Make and fill array   vals = new int[n];   for (int i = 0; i < n; i++) {     vals[i] = i + 1;   }   // Random permutation   for (int i = 0; i < n - 1; i++) {     int x = static_cast<int>(random() * (n - i));     swap(vals[i], vals[i + x]);   }   // Prove that it works and clean up   for (int i = 0; i < n; i++) {     cout<< vals[i] <<endl;   }   delete [] vals; } double random() {   return static_cast<double>(std::rand()) / RAND_MAX; }```
• 06-14-2004
Thantos
But what if you only wanted 20 numbers with a max value of 10000?

And I wasn't going for efficiently but ease of use and understanding for the questioner :)
• 06-14-2004
Prelude
>But what if you only wanted 20 numbers with a max value of 10000?
Then I would use a more suitable data structure than a simple array. ;) But that wasn't the question. The question was a list of numbers from 1 to N in random order.
Quote:

I need to generate a list of numbers from 1 (to a set number, for e.g. 25) in a random order.
>And I wasn't going for efficiently but ease of use and understanding for the questioner :)
I know, but I can't resist giving you a hard time every now and then. It keeps you on your toes. ;)
• 06-14-2004
laserlight
Quote:

I'm not sure why Preludes indicates that rand()/RAND_MAX is less likely to give a string of equal values on repeated values than rand()/modulus
Quote:

Using the low order bits of a random number may not result in a good distribution. I've seen long strings of repeating numbers when using modulus with rand.
From what I see, Prelude is not talking about repeating values, but about the random distribution.
Her argument is that dividing by RAND_MAX conserves this distribution, while the modulo method often does not.
• 06-14-2004
JasonD
Quote:

Originally Posted by weirdbeardmt
I need to generate a list of numbers from 1 (to a set number, for e.g. 25) in a random order. However, I can't have the same number twice, but I need the list ordering to be random.

The easiest way is this:

1. create the list storing 1..25, in order, in the indices 0..24
2. loop through the indicies, i = 0..24, pick a random index, j = 0..24, and swap the two.

This way, you swap a random index with each position, assuring that each index is swapped out at least once.
• 06-14-2004
laserlight
hmm... I did not see before posting that this thread was already several pages in length.
Anyway...

Quote:

Any deterministic random number generator is predictable, i.e. rand().
Well, they are deterministic :D

Quote:

There are devices which you can plug into your computer in order to generate non-deterministic random numbers.
Incidentally, Hotbits at fourmilab.ch provides such a generator, online.

For a pseudo-random number generator, I tend to use the Mersenne Twister, with a particular C++ implementation by Rick Wagner.
• 06-14-2004
Sang-drax
The problem with rand()%n using only the lower order bits is only a problem when n is a power of two.
Thus, the problem with rand()%n is not that much of a problem.

If you write rand()%3, the higher-order bits should also be taken into consideration.

Am I not correct, Prelude?

Quote:

There are devices which you can plug into your computer in order to generate non-deterministic random numbers.
I have made an encryption program that uses noise from the microphone compressed with MD5 to generate keys of many megabits.
http://www.strandmark.com/otp.shtml
• 06-14-2004
laserlight
Quote:

The problem with rand()%n using only the lower order bits is only a problem when n is a power of two.
Thus, the problem with rand()%n is not that much of a problem.

If you write rand()%3, the higher-order bits should also be taken into consideration.
I think that this is still a problem, since they arent always "taken into consideration" evenly.
Show 80 post(s) from this thread on one page
Page 3 of 4 First 1234 Last