Same Random Numbers Every Loop Iteration

Same random numbers every loop iteration

When you call srand(x), then the value of x determines the sequence of pseudo-random numbers returned in following calls to rand(), depending entirely on the value of x.

When you're in a loop and call srand() at the top:

while (...) {
srand(time(0));
x = rand();
y = rand();
}

then the same random number sequence is generated depending on the value that time(0) returns. Since computers are fast and your loop probably runs in less than a second, time(0) returns the same value each time through the loop. So x and y will be the same each iteration.

Instead, you only usually need to call srand() once at the start of your program:

srand(time(0));

while (...) {
x = rand();
y = rand();
}

In the above case, x and y will have different values each time through the loop.

Why does my random function generate the same number every time I call it in a loop?

Seeding repeatedly with tight timings will generate the same initial result after-seed every time. If seeding once per process isn't an option due to caller locality, then you should provide an initial static-state instead. What may work for you is this:

#include <random>

double Utils::randomNumber(int min, int max)
{
static std::mt19937 rng{ std::random_device{}() };
std::uniform_real_distribution<double> dist(min, max);
return dist(rng);
}

Note there is an intrinsic problem on the potential ceiling of the uniform real range, and it may be applicable to your usage (not likely, but never say never). See the notes here for more information.

Random number generator in a for loop gives same numbers each time

You are re-seeding your random number generator every time you call the function spinwheels.

Move these three lines into the top of your main function.

   time_t seed;

time(&seed);
srand(seed);

When we generate random numbers using rand(), we are actually using a pseudo-Random Number Generator (PRNG), which generates a fixed sequence of random-looking values based on a particular input called a seed. When we set the seed, we are effectively resetting the the sequence to start at the same seed again.

You might think that using time would result in a different seed each time, which should still give you a different result each time, but in a fast computer program, so little time has passed that the seed is effectively unchanged during each call.

That is why, as the other answer mentions, you should only call srand() once in your program.

Debug console prints the same random number for each loop iteration

This is caused because the random number is created outside of the for loop and won't change because it is created only once.

Instead create the random number inside your for loop

var array = [Int]()

for _ in 1...4 {
let randomNumber = Int.random(0...3)
array.append(randomNumber)
}

print(array)

Random Function generating the same value every time in a loop

You are presenting the same list instance each time.

my1 = [33,33,34,0,0,0,0,0,0,0]
store = []

for i in range(1000):
store.append(stochastic(my1[:]))

You should copy the list each time and then store will be filled with different data.

Random number in a loop

Move the declaration of the random number generator out of the loop.

The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, ...

Source

By having the declaration in the loop you are effectively calling the constructor with the same value over and over again - hence you are getting the same numbers out.

So your code should become:

Random r = new Random();
for ...
string += r.Next(4);

How to generate different random numbers in a loop in C++?

Don't use srand inside the loop, use it only once, e.g. at the start of main(). And srand() is exactly how you reset this.

random.randint using the same seed whilst in a loop

Your code repeats until 10 random ints all set the same position in numbers . That sets success and you try again. The problem is that you never clear success so while success == 0: is not True and the loop doesn't run. The solution is to reset success in the outer loop.

success = 0
times = 0

for i in range(0,10):
while success == 0:

numbers = [0,0,0,0,0,0,0,0,0,0]

for j in range(0,6):

x = int(random.randint(0,6))
numbers[x] = 1

count = numbers.count(1)

if count == 1:
success = 1
else:
times += 1


print(times)
print(numbers)
success = 0 # <== the fix

Trying to produce different random numbers every time a loop is executed

The issue is np.random.seed(-28) is just seeding the random generator [Documentation] , it does not return any random numbers , for getting a random number use - np.random.rand() .

Example -

return float((Lambda/(AvgSRate**Exp))*(1+(1/10)*(2*(np.random.rand()) - 1)))

If you want to seed the random generator, you can call it before calling the for loop that calls the Initilization function.

The function after this should look like -

def Initilization(AvgSRate,Lambda,Exp):
return float((Lambda/(AvgSRate**Exp))*(1+(1/10)*(2*(np.random.rand()) - 1)))

Also, one more issue, is that you are not adding the value returned from the function call to the list - Elist.append(Initilization) , you are just adding the function reference.

you need to change that to Elist.append(Initilization(<values for the parameter>)) to call the function and append the returned value into EList .



Related Topics



Leave a reply



Submit