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

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

Related

How do I make a counter to show the level I am on in a question game?

I need to make a math question game that asks a random question per level. So starting at level 1 it would ask a question and if answered correctly then the user can move to level 2 and so on. I am not sure how to make a counter that keeps raising the level as the questions are answered i.e. level 1, 2,3 etc. and at the end state what level the user got to? I know the basic code to start it but not sure what to do after this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
int i;
for(int i=0;i<1000;i++)
Remove the first int i;. You can use the i that is already initialized in the loop. This i can be used inside the for loop.
for(int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
This will print numbers 1-10. Can you solve the problem knowing this?

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.

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

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.

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