This question already has answers here:
srand() — why call it only once?
(7 answers)
using rand to generate a random numbers
(6 answers)
Closed 1 year ago.
I'm using the rand function in my program in a for loop but it generates the same number all time.
for(a=0, a<10; a++){
srand(3);
int sleep = (rand() % 400) + 500;
}
It always generates 683 or something like that. What's the reason? How can I fix that?
Use the call of the function srand before the loop like for example
#include <stdlib.h>
#include <time.h>
//...
srand( ( unsigned int )time( NULL ) );
for(a=0, a<10; a++){
int sleep = (rand() % 400) + 500;
//...
}
Otherwise you will get the same values according to the call srand( 3 ); because "if srand is then called with the
same seed value, the sequence of pseudo-random numbers shall be repeated." (the C Standard, 7.22.2.2 The srand function).
You are calling srand from inside of your loop. Each time you call it, you are setting the seed that is used to generate random numbers.
Move it outside of your loop as such:
srand(3);
for(a=0, a<10; a++){
int sleep = (rand() % 400) + 500;
}
Additionally, each time you run your program, it will generate the same sequence of numbers. If you wish to avoid that, you'll need to pick a different seed for each run. Typically this is a number that changes each time, such as time of day in seconds or something more unique.
srand((unsigned int)time(NULL));
Related
This question already has answers here:
Random numbers in C
(10 answers)
How can I generate different random numbers for each player?
(3 answers)
Closed 4 years ago.
So I was creating a program that would call a function and return 0 or 1 (0 meaning tails and 1 meaning heads) and then use that to print the outcome of 100 flips.
It seemed simple enough thinking I could use srand(time(NULL)) to seed rand() with constantly varying seeds. Here was my first crack.
#include <stdio.h>
#include <stdlib.h>
int flip();
int main(void) {
int heads = 0;
int tails = 0;
for (short int count = 1; count <= 100; ++count) {
int number = flip();
if (number == 0) {
printf("%s", "Tails");
++tails;
}
else if (number == 1) {
printf_s("%s", "Heads");
++heads;
}
}//end for
printf_s("\n%d Tails\n", tails);
printf_s("%d Heads", heads);
}//end main
int flip(void) {
srand(time(NULL));
int number = (int)rand();
printf("%d", number%2);
return number%2;
}//end flip
I would run the program and my rand() value would always be a five digit integer repeated in each iteration of the for statement (i.e 15367, 15745, or 15943).
I messed around until I discovered changing srand(time(NULL)) to srand(time(NULL)*time(NULL)/rand()) did the trick.
My only thought is that the time between each for iteration is so small the the time(NULL) part of the srand() function doesn't change enough to feed a different seed value.
I also tried srand(time(NULL)/rand()), however, this produced the same result (52 heads 48 tails) every time I ran the program (20+times); however, the rand() values were all different from each other.
I do not know why these things happened, or why the final srand(time(NULL)*time(NULL)/rand()) function worked, and I would love it if someone could explain!
The reason is, that time(NULL) changes only once per second!
This means, that you seed the random number generator 100 times with the same seed.
A better way is to seed the RNG only once at start of the process (at the head of main(), then you should get different values.
If you start your program more often than once a second, you could also seed it with
srand(time(NULL)+getpid());
or similar.
This question already has answers here:
srand() — why call it only once?
(7 answers)
Closed 5 years ago.
I am trying to implement multiply-shift algorithm for hashing and need to generate a new random number every time. I used a function to do the same as follows-
long mulShift(long x)
// returns h(x) for multiply shift function
{
srand (time (NULL));
long a = rand() % (long)pow(2,u);
if ((a>>1)<<1 == a)
a = a + 1;
long h = ((a*x) >> (u-k));
printf("%ld\t%ld\n", x, a);
return h%m;
}
Here u and k are global variables. If I call this function in a loop as follows-
for (int i = 0; i<5; i++)
mulShift(15);
I get the following output-
15 528638629
15 528638629
15 528638629
15 528638629
15 528638629
However, if I use srand before a for loop as follows-
srand(time(NULL));
for (int i = 0; i<10; i++)
{
printf("%d\n", rand()%1000000);
}
The output changes as follows-
638629
290058
512341
826358
80629
Why is this difference in behaviour? If I keep srand() inside the for loop in the last example, it again starts printing same values again and again. I apologise in advance if this is a stupid question.
Also, I'm using GCC on Ubuntu, if it makes a difference.
Here's the implementation of srand() and rand() in MSVC.
unsigned long rand_key;
void srand(unsigned long seed) {
rand_key = seed;
}
int rand(void) {
return ((rand_key = (rand_key * 214013L + 2531011L)) >> 16) & 0x7FFF;
}
Implementations on other platforms may differ, but they're essentially the same (calculating the next number from the current number). So calling srand() with the same seed is guaranteed to generate the exactly same sequence.
You can mix some stuff up to get a better seed:
#include <time.h>
#include <unistd.h>
srand( (unsigned)time(NULL) ^ getpid() );
This question already has answers here:
srand() — why call it only once?
(7 answers)
Closed 5 years ago.
I have made a function that creates a random number between 0 and 9 but when i call the function in main in a for loop with index i = 0 ; i < n ; i++ it prints 1 random number n types. but i want different values to be printed n times.
here is the code which i made :-
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void random_variable() {
int i;
srand(time(NULL));
i = rand()%10;
printf("%d ",i);
}
main() {
for(int j=0;j<10;j++) {
random_variable();
}
}
output is this :-
please click on the link to see the output :-
here instead of 8 ten times i needed 10 different values
srand() initializes the PRNG (pseudo random number generator) of your standard C library. A PRNG works by applying more-or-less complicated arithmetic operations to a value stored internally and returning part of the result as the next "random" number. srand() sets this internal state.
If you call srand() every time in your function, you won't get (sensible) random numbers because the internal state is reset every time. Call it once your program starts.
srand() seeds your random numbers. You want to do this once, not every time.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void random_variable() {
int i;
i = rand()%10;
printf("%d ",i);
}
main() {
srand(time(NULL));
for(int j=0;j<10;j++) {
random_variable();
}
}
This question already has answers here:
srand() — why call it only once?
(7 answers)
Closed 5 years ago.
I tried to put different random number element in each array.
However that 2 arrays got exactly same 10 random number.
What's wrong with me and how can I solve this?
#define MAX 100
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void number(int* array)
{
srand((long)time(NULL));
for (int i = 0; i < 20; i++)
array[i] = rand() % MAX + 1;
}
void printing(int* p_array)
{
for (int i = 0; i < 20; i++)
printf(" %d", p_array[i]);
puts("");
}
int main(void)
{
int score1[20];
int score2[20];
number(score1);
printing(score1);
number(score2);
printing(score2);
return 0;
}
srand((long)time(NULL));
When you are doing this, you are initializing srand with a seed based on the time the line has been called. The main point of a pseudo-number generator such as srand is that, whenever the seed is the same, the generator will always return the exact same serie of values, so a program using the generator with a specific seed can reproduce the exact same results several times.
The problem is that you initialize srand two times, and that the line is run two times at the exact same time (at least at the same second on such a short program), so the seed is the same. Your two arrays are, thus, strictly identical.
Call this line only once at the beginning of your main function, and you will use only one serie of numbers instead of two identical ones, leading to your two arrays having different values.
int main(void)
{
srand(time(NULL));
int score1[20];
int score2[20];
number(score1);
printing(score1);
number(score2);
printing(score2);
return 0;
}
Remove this statement from the number function
srand( (unsigned int)time(NULL));
and place it in main before the calls to number.
Otherwise the expression time(NULL) could yield the same value.
As 4386427 says, the problem is your call to srand(null). It resets the random number generator. If it didn't generate the same number series, then your number generator is broken. Remove it or call it with say the current time will fix the problem.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
calling rand() returning non-random results
In my workshop I need to takes 2 different random numbers but I get 2 same random number.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int random1_6(){
int k;
srand(time(0));
k=((rand()%6)+1);
return k;
}
int main(void){
int a,b;
a=random1_6();
printf("%d.\n",a);
b=random1_6();
printf("%d.\n",b);
return 0;
}
How to get 2 different random number?
A non-cryptographic random number generator (RNG) is not truely random but it generates random-like numbers based on a seed.
What you do is initializing the RNG with the same seed two times, so you get the same results. Seed the RNG just once, e.g. at program start, and you will get random-like different results.
Edit: a code like follows should work:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int random1_6(){
return ((rand() % 6) + 1);
}
int main(void){
int a,b;
srand(time(NULL));
a = random1_6();
printf("%d.\n",a);
b=random1_6();
printf("%d.\n",b);
return 0;
}
Don't do srand(time(0)); on every call. Only call it once.
You must initialize the random number generator seed with srand() only once : upon each call you are re-initializing the RNG with the same seed since is it most likely that the two subsequent calls to time(0) will return the same timestamp (seconds-level precision), hence rand() will return the same number twice.
If you call srand() only once at the beginning of your program (in the main() entry-point), then every call to rand() will return a different number.
You always initialize the random number generator with the same seed, so you'll get the same random sequence, it is pseudo random anyway. Typically you will only want to call srand once in the beginning to initialize the generator.
Also, you only have 6 different possible outcomes, so it is perfectly legitimate to get same number twice, there is 1/6 chance for that.