![]() |
C - random number generator
I'am looking for a random number generator using the C language wich can generate 100 random ints. The ints must be stored in an array.
Can anyone help me? |
Here is a state of the art random number generator. Practically industrial strength. The output comes out in the form of a uniform random variable though, so if you need other probability distributions, you have to obtain them from the uniform variable in the usual way (check out simulation textbooks and resources if you need the 'recipes'). It is also not hard converting uniform r.v's into integers. just do a ceiling on the r.v.
Also, it only outputs a single number at a time, but I'm sure you can put it into an array yourself. Check out the RngStream link from here.... http://www.iro.umontreal.ca/~simardr/ Here are the links to the C and C++ files themselves. http://www.iro.umontreal.ca/~lecuyer/myftp/streams00/ |
oh... remember to credit the authors in your program. It would be stealing otherwise.
|
why not just generate numbers w/ stdlib.h then put them into an array?
Quote:
|
I'll try both your suggestions, thanks for your help. I'll let you know if if works.
|
Be careful when using rand in c/c++... only generate the seed once! I'm sure there are tons of example code out there.
|
The rand command works!
Check out the source code: #include <stdio.h> #define MAX 100 int a[MAX]; int rand_seed=10; /*Returns random number between 0 and 32767.*/ int rand() { rand_seed = rand_seed * 1103515245 +12345; return (unsigned int)(rand_seed / 65536) % 32768; } int main() { int i,t,x,y; /* fill array */ for (i=0; i < MAX; i++) { a[i]=rand(); printf("%d\n",a[i]); } return 0; } Comment: #define MAX 100, the size of the array is 100. int rand_seed=10, by changing the the value of the seed you will get an array with other random ganerated numbers. Thank you all for helping me! |
a thread on random number generators and no has mentioned that it is in reality only possible to create a pseudo random number!!! the pedantic of us must have missed this one!
|
Quote:
I was going to mention pseudo-random numbers. Really. I was :p |
I was hoping no one would mention this.
I mean, if the guy doesn't even know how to generate numbers, pseudo-random or otherwise, how much can he really care about the distinction? |
dnd: I figured that was a given :shrug:
|
Actually, if you wanna make a really badass random number generator you should throw in a little entropy. For example, instead of using your constants: 1103515245, 12345, & 65536, you could form a function based on current disk i/o, cpu usage, cpu temperature, fan speed, etc... A little bit of hardware randomness will get you closer to truely random. ;)
|
...what we REALLY need is a geiger counter and a radioactive isotope...
ummm, never mind. |
Quote:
|
All times are GMT -8. The time now is 08:11 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project