Hi,
I need to generate 500 random numbers having uniform distribution. Does rand function ensure uniform distribution?
Thanks,
edesign
Hi,
I need to generate 500 random numbers having uniform distribution. Does rand function ensure uniform distribution?
Thanks,
edesign
Do you mean something like this: 2 8 10 6 12 4 ?
The only random element of that is the order. In that case, I would just make an array of evenly distributed numbers (eg, generated with some function), and then use rand() to select the element number.
Last edited by MK27; 08-15-2009 at 10:47 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
The standard doesn't guarantee a uniform distribution (it just says that rand gives a sequence of pseudo-random integers) but I am not aware of any compiler that doesn't have a uniform distribution for rand.
If we assume that "uniform" means "it's roughly the same likelihood of getting all numbers in the expected range", then I'd agree with Tabstop. rand() is not perfect, but for many things it's good enough. And this sounds like one of those cases.
If you intend to make money using the random numbers, then you may need something better (less predictable for example), so that someone playing your card game or whatever can not predict what the next set of cards will be, for example.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I want it to be uniformly distributed precisely. If I generate a histogram, I want it to be flat, same count for all the numbers. I need to generate the numbers in [0,1]. If rand generates same number more times than it'll destroy Uniform Dist property and will lead to errors in further calculations. The only way I can think of avoiding it is to check for every value, but that will be too costly to do. Is there some other way to do that?
Even if rand() gives uniformly distributed numbers (which it does) that does not imply that you will get a flat histogram, nor should you expect it to, nor should you desire it to be so.
I think the only way to do that would be a rand() which guaranteed no repetition until all possibilities have occurred, which is not exactly random. When you roll a die, there is some chance of rolling 3 sixes in a row. If you rolled 100 times, I suppose the distribution would be reasonable uniform, but for this reason you cannot ask it to be perfect -- that's not random.
Going back to my earlier idea, you could put all numbers into an array, shuffle it using rand(0), and then pop them off one at time from this "random list"; when you reach the end, shuffle and start again. No matter how many times you do that, you will end up with a dead even number of repetitions for each value, but the order in which they occur will be random.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
In other words, when you generate say the 400th random number it is absolutely not allowed under any circumstance to be equal to one of the previous 399 numbers?
If so, then you can't just use a PRNG directly. You need an algorithm. The good news is that when done cleverly the cost of checking that the number hasn't been generated already becomes near zero.
The trick is to first set up an array of the items you want to include in your output, pick one at random, and then overwrite it with one from point n, decreasing the length of the portion you are allowed to pick numbers from.
My C is a little rusty, I'm used to C++ but...
Including the appropriate headers is left as an exercise fo the readerCode:int main(void) { /* setup */ float values[500]; int n = 500; int pos; for (int i=0; i<500; ++i) values[i] = i / 499.0; /* generation */ srand(time(NULL)); while (n > 0) { pos = rand() % n; printf("%f\n", values[pos]); n--; values[pos] = values[n]; } }
That should give 500 equally spaces values in the range [0,1] assuming you didn't actually mean the range [0,1).
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
If I use the approach suggested by iMalc, it will work well for part 1 but when the experiement is repeated it will generate the same set of 500 numbers each time. Just in a different order. But thanks all for suggestions. I think I am confused about the question itself.The question is like this:
Write a function to generate random numbers as follows: Everytime the function is called, it generates 500 new random numbers in U[0,1] and output their sum.
Generate 50,000 random numbers by repeatetedly calling the above function and plot their normalized histogram.
Thanks,
Edesign