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. |
640K is enough for anybody.
|
Quote:
I can't tell you why, but I like Code:
seed ( getpid() ^ time() ); Code:
seed( tv_sec * tv_usec (microseconds)); |
Having looked through the thread it would appear that the answer I was going to give to the original problem has already been given by Johnsfine.
However, the digression on how you seed the random numbers raises a few thoughts. To me I don't like the idea of using the PID because first it is not cross platform. I also like to keep a record of the seed so that the process can be recreated so I tend not to use time() either rather a variable that could be read in or derived (possibly from the time function). But basically if having "random numbers" is a critical element of a program then don't use the rand function but look for a means of getting true random numbers. |
Quote:
|
Quote:
I didn't notice the fact that the thread was reopened with a different question, nor the dates on the earlier posts. I just saw that there seemed to be no answer posted for the original question. I really wish the forum software would give some very visible indication when there is a giant jump in date between two successive posts in a thread. |
need help
this random number program isnt working anyone know what im doing wrong?
Code:
#include <gtk/gtk.h> |
All times are GMT -5. The time now is 04:03 AM. |