Rand() Returns Same Values When Called Within a Single Function

rand() returns same values when called within a single function

You shouldn't call srand() before each call to rand(). Call it once – somewhere at the start of your program.

The problem is you restart the random generator so it starts to produce the very same pseudorandom sequence from the very same point.

Rand() returns same or very similar output values

To make a random number with a nearly the same seed(time), you can add a static variable to make rand() behaves different even with same parameter; or, you can make the parameter change when you get same time. For example:

int t=0;
...
rand(t=(t*7)^time(NULL));

rand() returns the same value, even after a srand

I was using srand() more than one time in my program, now I call it once in my main() function and that works fine.

Why do I get the same result with rand() every time I compile and run?

You need to seed the rand function with a unique number before it can be used. The easiest method is to use time()

For example

srand(time(NULL));
rand();//now returns a random number

The reason is that the random numbers provided by rand() (or any other algorithm based function) aren't random. The rand function just takes its current numerical state, applies a transformation, saves the result of the transformation as the new state and returns the new state.

So to get rand to return different pseudo random numbers, you first have to set the state of rand() to something unique.

rand() produces the same result on each function call (with srand(time(0))

(Converting my comment to an answer).

For most applications, you'll only really want to seed rand once in the course of running a program. Seeding it multiple times requires you to get different random seeds, and it's easy to mess that up.

In your case, the time function usually returns something with resolution on the level of seconds (though this isn't actually required by the standard). As a result, if you call time twice within the same second, you might get back the same value. That would explain why you're getting duplicate values: you're seeding the randomizer with the same value twice and then immediately querying it for a random number.

The best solution to this is to just seed the randomizer once. Typically, you'd do that in main.

If you really do want to seed the randomizer multiple times, make sure that you're doing so using a seed that is going to be pretty much random. Otherwise, you risk something like this happening.

rand() function in C return the same value

Given the same starting point, repeated calls to rand will always generate a predictable stream of values. The way you change this is by seeding the generator with a value which varies. You do this by calling srand.

Assuming you don't need to run your program more than once a second, the current time would be a cheap/easy way of choosing a seed.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int val = rand();
printf("val = %d\n",val);
}

I've made another small change to your program. Output is sometimes line buffered so I've added a newline \n character to the end of your printf string.

Why does rand() always return the same value?

You need to give a different seed, for example:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int a;
srand ( time(NULL) );
a = rand();
printf("%d",a);
return 0;
}

Why rand() returns the same values when I run my program in a script ?

The time() call has only whole-second precision. If your programs all run the same second, they will all use the same seed.

You must add more entropy. Consider using the return value of getpid() if you have it, or else investigate the platform's random sources.



Related Topics



Leave a reply



Submit