Function call inside a loop in C [duplicate] - c

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.

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

Why do we use 'NULL'? [duplicate]

This question already has answers here:
What is time(NULL) in C?
(8 answers)
Closed 3 years ago.
Why do we use 'NULL' in code below.
Why can't we multiply the seed by an integer?
Sorry I am new to C++.
CODE
srand(time(NULL));
The time function can write the time to a location provided by a pointer to the function call. This pointer argument can be a null pointer, and then time only returns the current time.
On most systems the time function returns the number of seconds since an epoch, and as such is a pretty unique integer value to use for seeding the random number generator.
The single statement
srand(time(NULL));
is equivalent to
time_t temp_time = time(NULL);
srand(temp_time);
Or if we want to use a non-null pointer
time_t temp_time;
time(&temp_time);
srand(temp_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 this program not run infinitely? [duplicate]

This question already has answers here:
Values obtained in case of a recursive function
(3 answers)
Closed 9 years ago.
Shouldn't this program run infinitely because main is being called every time? And why it's output is 0 0 0 0? I know it's a noob question but I am not able to get it. What --i do and what is the effect of declaring i as static?
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
The static int is not reinitialized each time. Thus, each time main() is called, i is one lower.
when (--i) is zero, the recursive loop terminates.
Thus, it starts off with i=5, and calls a new copy. This one has i=4, which again calls a new copy. This continues until i=0, at which point the function just terminates. Control flow is then returned up the call stack, and each copy of main prints i, which is now 0. 4 copies means 4 zeroes.
The reason it does not run forever is because at some point i becomes 0 and no longer calls main. The trick is static which references the same memory location and hence is decremented each time main is recursively called.

Resources