Random number generator for linux
i have a Random number generatorfor win32 but when i compiled it and ran it on linux it always gave 0 as the generated number but on windows it gives a differnt number!?
can anyone help? Code:
int num; |
hi
try to assign and print out as a double or float and use %f instead of %d, it might help. ;) |
i tred that it still did not make a differnce
does anyone know of any good randum number generators that if givvena number say 37 would generate a number between 1 and 37?? |
hi
the main problem of your prog is that you have an overflow!!! Just try something like : int num; srand(time(NULL)); return (int) (((double) rand() / (double) RAND_MAX) * (double) limit) + 1; by ;) |
Anything wrong with using this?
srand(time(NULL)); array[i] = rand() % 1001; |
Sorry about previous post. I am adapting this from previous code.
Anything wrong with using this? int num; srand(time(NULL)); num = rand() % 38; return num; |
Or if you wanted to create a random number from 1 to limit, try this:
int num; int limit; srand(time(NULL)); num = (rand() % limit) + 1; return num; |
Dont mean to sound stupid but what does the % do i have looked it up in the book that i have and cant find it? well not used in this way.
|
The % is called the modulus. It gives you the remainder of dividing two numbers.
Thus, 13 % 10 returns 3. Using this is nice because: ( number % limit ) + 1 always returns a number from 1 through the limit. |
OK habiblove,
you're right : modulus seems to be much easier... sorry ! :study: :study: :study: |
It is pretty neat. Who said a college education isn't worth anything?
|
i went to college but din't get all that much out of it
|
main()
{ printf("I have no idea!\n") } *****Colonel Panic***** |
All times are GMT -5. The time now is 11:04 PM. |