Why use srand when rand() generates all random numbers in C [duplicate] - c

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.

Related

How to create random number values in c [duplicate]

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);
}

How to use random() in C [duplicate]

This question already has answers here:
How to generate a random int in C?
(32 answers)
Closed 6 years ago.
So I am currently learning C, and I have some confusion about how the random() function works.
I know I have to provide a seed, but I don't know how to actually generate the random number.
srandom(seed);
long int value= random(40);
When I try this it gives me a compiler error:
too many arguments to function ‘long int random()
The end goal is to get a random number between 1 and 40.
I should add that the goal is to not use the rand() function.
The answer to your question is precisely answered by the compiler.
You are passing '40' to the function random() which it is not supposed to receive.
The signature for random() is:
long int random (void)
void keyword here is indicator that the function in hand is not supposed to receive anything.
Here's an example program to generate 6 random numbers.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int max = 5;
for (int c = 0; c <= max; c++){
num = random();
printf("%d\n",num);
}
return 0;
}
Output:
fire-trail#Sahai-Yantra:~$ gcc try.c
fire-trail#Sahai-Yantra:~$ ./a.out
1804289383
846930886
1681692777
1714636915
1957747793
424238335
To reach your end goal of generating random numbers within a given range using random(), see How to generate a random number from within a range.
The answer is right there in the error thrown by the compiler
too many arguments to function ‘long int random()
Change random(40) to random()
There is no argument in random() function.
See man page of random(). Prototype of random()
long int random(void);

Function call inside a loop in C [duplicate]

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.

Creating a random array of Bits [duplicate]

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.

C program for generating random numbers [duplicate]

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.

Resources