![]() |
generating random numbers in C
Just curious. When I created a 5x4 array and filled it with random numbers using the rand() function, I noticed the last number in the row (row 0, column 4) is identical to the first number in the next row (row 1, column 0). Also, each time I run the program, it generates the same numbers when I run the program. Shouldn't the program be generating different numbers?
|
Hmmm - "srand()/rand()" isn't exactly the most sophisticated random number generator in the world, but it *should* work for your purposes.
Please compare what you're doing with this: Code:
/* |
Re: generating random numbers in C
Quote:
Code:
j = 1 + (int)( 10.0 * rand() / ( RAND_MAX + 1.0 ) ); |
If you use just time() to get pseudo random numbers and if you run your program many times in a row, then it's predictable that you will have the same sequences.
It's preferable to use gettimeofday() multiplying tv_sec * tv_usec (microseconds). |
Re: generating random numbers in C
Quote:
|
Quote:
|
Quote:
John G |
Quote:
|
Quote:
Quote:
4 columns means columns 0 .. 3. There is no column 4. If you think your 5x4 was 5 columns and 4 rows, I think you are mistaken. Your symptoms say you have 4 columns. The fact that you are using the array for random numbers just distracted you from an issue that is in the structure of the array itself. |
Quote:
|
There is nothing to correct rel0aded. As JohnGraham told you, if you omit the type the compiler will automatically expect an integer to be returned. It is just a good practice to state what type you expect a function to return, thus (most) people declare main as returning int...
|
Quote:
Code:
#include <stdio.h>and worked :) |
Quote:
i think that is overboard. |
Quote:
|
how can you even call a program 1000000 times per second?
bash, batch and C system and exec calls are all too slow. the only way you could do it is if its from withen it self and in that case it would have the same PID. |
| All times are GMT -5. The time now is 12:50 PM. |