C program for generating random numbers [duplicate] - c

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.

Related

How do I generate a unique set of random variables [duplicate]

This question already has answers here:
Why does rand() yield the same sequence of numbers on every run?
(7 answers)
Closed 2 years ago.
My problem is really simple. In C, I am trying to create a set of random values to set for r, however whenever I run the code it generates the same numbers over and over again rather than a unique sequence of numbers on every iteration. How should I change the code to fix this?
My code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
int r;
for (int i;i<5;i++)
{
r=rand() % 10;
printf("%d\n",r);
}
}
This code always returns the values 1,7,4,0,9. How can I make it so that it instead randomizes each on every successive use of the function?
rand does not generate real random numbers. But to make the unique you need to seed it with something which will be different every time you run the program.
Example:
int main()
{
int r;
srand(time(NULL));
for (int i;i<5;i++)
{
r=rand() % 10;
printf("%d\n",r);
}
}

Need help implementing total random characters in C [duplicate]

This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Generating a random bit - lack of randomness in C rand()
(4 answers)
Closed 3 years ago.
I am creating a program that prints out 1000 random occurrences of the letters "H" and "T" and my code implements rand()%2 but as i can see from running this program (code below) that its not completely random (the letters are random but always the same with every execution). I want to establish a more effective way by implementing RANDMAX to make every case of the program running completely random, how would I be able to do this?
#include <stdio.h>
#include<stdlib.h>
int main()
{
int i=1,n;
char ch ;
for( i = 1; i <= 1000 ; i ++ )
{
n = rand()%2;
if(n==0)
ch = 'T';
else
ch = 'H';
putchar(ch);
}
return 0;
}
Just add
srand(time(NULL));
after
int main()
{
That will initialize the random generate with a new seed every time you run the program. In this way, you'll get a new random sequence every time.

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

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.

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.

Why does rand() always return the same value? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random numbers in C
using rand to generate a random numbers
I'm trying to generate random numbers but i'm constantly getting the number 41.
What might be going so wrong in such a simple snippet?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a = rand();
printf("%d",a);
return 0;
}
Thanks for help.
You need to give a different seed, for example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int a;
srand ( time(NULL) );
a = rand();
printf("%d",a);
return 0;
}
You need to seed the generator.
This is expected. The reason is for repeatability of results. Let's say your doing some testing using a random sequence and your tests fails after a particular amount of time or iterations. If you save the seed, you can repeat the test to duplicate/debug. Seed with the current time from epoch in milliseconds and you get randoms as you expect ( and save the seed if you think you need to repeat results ).

Resources