How to use random() in C [duplicate] - c

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

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

"warning: division by zero [-Wdiv-by-zero]" when not dividing by zero [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 3 years ago.
My code for the following simple coding exercise produces a "division by zero" warning, and I'm wondering why.
#include <stdio.h>
int main() {
for(int i = 0; i < 100; i++) {
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
}
return 1;
}
temps.c: In function ‘main’:
temps.c:6:45: warning: division by zero [-Wdiv-by-zero]
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
I realised while writing this question that it's because I should have written 5.0/9.0, since C handles division with integers in a way that I didn't expect. Posting this anyway since I couldn't find this particular error linked to this particular problem on SO.

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.

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