1. ## random?

I completely confuse using rand(). Its generating same number, even using srand()...

Is there something like Math.random() in C?

2. rand() generates a seemingly non-related sequence of numbers initialized by srand()

So for the same srand() you are probably getting the same sequence of numbers. Or at least the same first number.
Use something like srand(time(NULL), or something similar so also the initialization depends on something random and changing.

3. Originally Posted by audinue
I completely confuse using rand(). Its generating same number, even using srand()...

Is there something like Math.random() in C?
How are you using srand() - rand() gives the same series when given the same seed (srand's argument is the seed for the random number).

--
Mats

4. I suggest that you read Prelude's articles on:
Using rand()
Random Numbers

5. you have to initialize it the proper way:

Code:
#include <stdio.h>
#include <time.h>

int main(void)
{
srand(time(NULL));
int i;
for(i = 0; i < 10; i++)
printf("%d\n", rand());
return(0);
}
should generate 10 different numbers. Well the chance of having rand() return the same value ten times in a row is extremely low

6. If you seed it with the same value, it will always return the same sequence.

If you dont seed it , it will always return the same sequence as the unseeded sequence.

If you seed it with a random seed, the sequence repeats after a very short period.

rand() is only pseudo-random.

7. srand(time(0)) will not work if we execute the application twice (fast)...

8. srand(time(0)) will not work if we execute the application twice (fast)...

9. Originally Posted by audinue
srand(time(0)) will not work if we execute the application twice (fast)...
Did you read what I said?

If you seed with the same value you will get the same sequence. If you execute realyl fast, time(0) wil return the same value, thus you will use the same seed value, hence you will get the same sequence.

10. Originally Posted by audinue
srand(time(0)) will not work if we execute the application twice (fast)...
Correct, since time(0) returns an value which is precise down to seconds, and if you run it twice in a row quickly, you get the same second (and even if it's only one or two more, it may not produce significantly different values).

If you really require that mode of operation (random numbers created each time the application runs and the application can be started several times the same second), then you need to use some other method of seeding. Some sort of high-precision time would be a good choice - but how you get that would be OS and/or compiler/runtime dependant.

--
Mats

11. laserlight:
I've tried their codes that provides the same results.

Hohoho... ^_^
I've got the simplest way!
Try: srand(GetCurrentProcessId())

Unfortunately its only in Windows...

EDIT:
Use _getpid() to replace GetCurrentProcessId().

Is it safe to do something extreme like: srand(_getpid() * time(0))?

12. Originally Posted by audinue
laserlight:
I've tried their codes that provides the same results.

Hohoho... ^_^
I've got the simplest way!
Try: srand(GetCurrentProcessId())

Unfortunately its only in Windows...
Yes, and current processid isn't particularly "random" either.

Better to get the low word of the TSC or QueryPerformanceCounter() for example. TSC will walk all the way through 32 bits within about 2 seconds on a modern machine.

--
Mats

13. I've tried their codes that provides the same results.
The point that I am getting at is to understand, not simply try out code hand outs.

One thing to ask is this: why is your program being executed multiple times in quick succession? If this is a normal mode of operation, then perhaps you should maintain state with a database.

14. No, I just want to make a real random number generator...

And I think using srand(time(NULL)) to create random number isn't a wise way, because the accuracy still using seconds (or milliseconds)...

15. No, I just want to make a real random number generator...
Consider a setup such as the one used for HotBits or the website that shawnt's Quantum Random Bit Generator gets its randomness from.

And I think using srand(time(NULL)) to create random number isn't a wise way, because the accuracy still using seconds (or milliseconds)...
That shows that you are ignorant because you have failed to read the articles pointed out to you. The accuracy (or more accurately: precision) does not matter, unless you are using the generator wrongly. The idea behind a pseudo-random number generator is to seed once (and perhaps again after the sequence is exhausted), then use the sequence generated. It is not to keep on seeding and then using the first number in each sequence.

EDIT:
If you are talking about the predictability of time(NULL), or that srand(time(NULL)) is not guaranteed to be portable as Prelude pointed out, or that the standard random number generator facility is often not of good quality, then that is another matter. Admittedly, the predictability of time(NULL) is related to its precision in seconds, but the point is that it is rand() that generates the random numbers, with srand() there to "select" a sequence to generate.