This question already has answers here:
Recommended way to initialize srand?
(15 answers)
srand() — why call it only once?
(7 answers)
Closed 2 years ago.
i am trying to create random data so i can review an algorithm in cache policy. When i use the first code with rand() i get the same numbers every time i run my code(i knew this was coming) so i tried to use srand. when i inserted it i get different numbers every time i run the code but the same 10 numbers at my loop. What am i doing wrong to create random data??? After that i will make with zip ununiformed data.
sorry my bad the loop is in main function:
for(counter=0; counter<10; counter++){
printf("Number %d is %lf\n", counter, rand_val());
}
And i am using this function to create numbers.
double rand_val(){
double rnum;
srand(time(NULL));
rnum=rand();
return(rnum);
}
Related
This question already has answers here:
How does srand relate to rand function?
(4 answers)
Closed 7 years ago.
I am generating random numbers , and someone told me to use srand. I searched about srand and found it is used on top of rand(). But I do not understand why not use just rand() since I get random numbers in my below code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int i=0;
for(i=0;i<5;i++){
printf("random number is %d\n",rand());
}
}
And I get a set of 5 random numbers. So rand() does gives random numbers. Why do we need srand on top of that then?
If you simply use rand() and run your code multiple times you will notice that you tend to get the same sequence of random numbers every time.
srand is used to seed the random number generator. This allows you to generate different sequences. I'd suggest that you read the manual page for srand.
This question already has answers here:
C program - srand() [duplicate]
(6 answers)
Closed 7 years ago.
int random(){//function for random number generation
time_t t;
srand((unsigned) time(&t));
int rand = rand()%468;
return rand;
}
I want to put this function in a loop so it assigns a random number to a variable during each iteration of the loop, so i figured i would try this:
for(x=0;x<n;x++){
y=random();
printf("%d\n",y);
}
However this doesn't work as i expected, y just gets the number from the first iteration and repeats it, can someone explain how i can force the function to recall so it will set a new value to y?
You should not call srand() everytime before calling rand().
The way you are calling srand() always passes the same value as the seed, because the difference in seconds between each call is 0, since it's a lot smaller than 1s.
Since you always call srand() with the same seed and right before rand(), you will get always the same value from rand().
Just call srand() once at the start of the program, so in every run there will be different random numbers generated.
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 7 years ago.
Using rand(), I'm trying to create an array which generates 32 random numbers each time I run the program. However my program below gives me the same 32 random bits each time. Is there anyway I can get it to generate different 32 bits each time I run the program?
for(a=0;a<32;a++)
{
ran[a]= (rand()%2);
}
You need to set up the random seed each time you run the program to something different to do that. What people do is usually this:
#include <time.h> /* time */
...
srand (time(NULL));
...
//Calls to rand();
Then your random seed is different at every start of the program. Check more details about srand here.
This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
Unique random number generation in an integer array [duplicate]
(9 answers)
Closed 8 years ago.
I want to randomize number in each element of array in a variabel. I currently use srand() function. But, with this function i could get a same number in two or more element of array.
the output of my program is
number[0]=6
number[1]=3
number[2]=8
number[3]=3
See, number[1] and number[3] has same value. How to prevent this thing happen?
This question already has answers here:
How to generate a random int in C?
(32 answers)
Closed 8 years ago.
I'm writing a code that will return a random number between 5 and 20, and i'm running into a problem where it will always produce the same number over again and i can't seem to solve it.
#include <stdlib.h>
#include <stdio.h>
int random = 0;
int randomnumbergen() {
srand(12345);
random = rand() % (20 - 15) + 15;
return random;
}
First, you'll want to call srand() at the beginning of your program one time.
Next, you'll want to replace
srand(12345);
with
srand (time(NULL));
You're using the same seed each time - producing identical results. You need to not hardcode it.