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.
Related
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);
}
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:
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.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is there any way to generate a random number in C without using seed.
Here what have so far but it still using srand(time(NULL)); which is a seed.
#include <stdio.h>
#include <time.h>
#include <math.h> /* required for sqrt() */
#include <stdlib.h> /* required for rand() */
int gen_rand(); /* note these are declarations of functions */
void main()
{
int number;
srand (time(NULL)); /* everytime you run program, it will give you different result */
number = gen_rand();
printf("%d is the power of 2 of %.0lf\n", number, sqrt(number));
}
/* Function generates random number power 2 of 20 - 230 */
int gen_rand()
{
int n;
n = rand() % 211; /* n is random number in range of 0 - 210 */
n = n + 20; /* n is now in range of 20 - 230 */
return(n*n); /* return n to the power of 2 */
}
Yes and no.
There are basically two methods to get even remotely random numbers in c.
1) have a pseudo random number generator with seed -- that is an algorithm, that produces some sequence of numbers using clever arithmetic operators and possible lots of internal variables that are mixed, permuted, twisted and whatever. The seed can be implicit (i.e. always zero and each time you run the program, the same sequence is generated). Or it can be explicit, where the seed can be changed somehow between runs.
2) Using external source of data, that somehow changes in between runs. That could come from a timer, environment variables (program id perhaps), noise from camera, mouse movements etc.
1+2) use the external source of noise as seed to pseudo random number generator.
All non-hardware based PRNG require some form of random input to combat their deterministic nature, thus a seed will always be required.
You can try abuse /dev/rand under linux (but it also is a PRNG), or if you have a very modern Intel CPU, their new digital RNG facilities would work.
No. If you don't seed the automatic number generator, it will behave deterministically and yield the same numbers every time.
Yes. Generating random numbers by using the rand() function without using a seed will give you the same set of random numbers though.