I need to generate 1000 random numbers inside a for loop.
my problem is that the random number generated is always the same. since im using time NULL to initiate the generator, why am i getting the same numbers? here is the code i used:
#include <stdio.h>
#include <stdlib.h>
#define LIMIT 30000
int main(){
int i;
srand((long) time(NULL));
for(i = 0; i < 1000; i++){
int x = rand() % LIMIT;
printf("%d\n", x);
}
}
If you run the program multiple times during the same second, you will pass the same value to the generator as seed. You have to wait at least a second before trying it again.
This is because the time function returns the number of seconds since a specific time, and if called multiple times during the same second will return the same value.
your code is right, but you forgot to include the time.h library.
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // <--- now it works
#define LIMIT 30000
int main(){
int i;
srand((long) time(NULL));
for(i=0;i<1000;i++){
int x = rand() % LIMIT;
printf("%d",x);}
Related
I want the program to print randomly one of these numbers:
{1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10}
Instead it prints randomly nonsense numbers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand( time(NULL) );
int card;
int deck[40] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10};
card = rand()% deck[40];
printf("%d", card);
return 0;
}
What can I do?
thanks
This simple way to do this is
card = deck[rand() % 40]
This way you are picking a 'random' index in the 40 numbers of your array.
Even tho with this array you have the same probability on every number.You can change the balance by changing the array.
I'm trying to get an array to be initialized in a function. The initialized array should contain a set of random numbers within a certain range when working as intended. Is this possible in C?
#include <time.h> //time func for seed init
#include <stdlib.h> //srand/rand funcs
#include <stdio.h> //Library for printf function
#define LIMIT 100
void myFunction(int array[], int arrayIndexMaxSize){
for (int index = 0; index < arrayIndexMaxSize; index++){
array[index] = rand() % LIMIT; //Where limit is randomly produced from 0 - Limit inclusive
printf("array[%d] of arrayIndexMaxSize(%d) = %d\n", index, arrayIndexMaxSize, array[index]); //prints out th$
}
}
void main(){
srand(time(0)); //initializes kinda random number generator
int array[50]; //If known at runtime or compile time, must be at block scope meaing inside {curly} braces of any$
myFunction(array, 50);
}
This question already has answers here:
Generate random double number in range [0, 1] in C
(2 answers)
Closed 2 years ago.
I would like to generate a random, real number in the interval [0,1].
I would like to set a pointer, say n, for the number so whenever I stated n, it will be referred to the random generated number.
I have searched on StackOverflow and on Google, but most of them are for C++ or for integers.
I have tried this code suggested to me in the answers:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double n;
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();
printf("%f", n);
return 0;
}
However, I can only get a value 0.00000000.
How could I fix my program?
You can use:
#include <time.h>
srand(time(NULL)); // randomize seed
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();
srand() sets the seed which is used by rand to generate pseudo-random numbers. If you don't call srand before your first call to rand, it's as if you had called srand(1) (serves as a default).
If you want to exclude [1] use:
(double)rand() / (double)((unsigned)RAND_MAX + 1);
Full solution:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
double get_random() { return ((double)rand() / (double)RAND_MAX); }
int main()
{
double n = 0;
srand(time(NULL)); // randomize seed
n = get_random(); // call the function to get a different value of n every time
printf("%f\n", n); // print your number
return 0;
}
Every time you run it you will get a different number for n.
This shows how to get random real numbers in the range 0..1 but please note that they are not uniformly distributed. There are only (RAND_MAX+1) discrete values.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int i;
double n;
srand((unsigned)time(NULL)); // seed the random num generator ONCE only
for(i = 0; i < 3; i++) { // get 3 random numbers
n = (double)rand() / RAND_MAX; // in the range 0 ... 1
printf("%f\n", n); // use correct format specifier for the var
}
return 0;
}
My program output:
0.622608
0.814081
0.878689
This question already has answers here:
rand() generating the same number – even with srand(time(NULL)) in my main!
(7 answers)
Closed 9 years ago.
How do I make my addRandomNumbers() function add different numbers to the array ? Right now it is just adding the same number.
I dont know what i did wrong, I had it outside of the for loop originally and it still generated the same number.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define clear system("cls")
#define pause system("pause")
#define SIZE 5000
#define LB 1 //this is the lower bound
#define UB 500 //this is the upper bound
//Lets Prototype
void addRandomNumbers(int n[]);
void bubbleSort(int n[]);
void displayRandomNumbers(int n[],int c[]);
int main(){
int numbers[SIZE]={0}, counter[SIZE]={0};
addRandomNumbers(numbers);
bubbleSort(numbers);
displayRandomNumbers(numbers,counter);
}
void addRandomNumbers(int n[]){
int i;
for (i=0; i < SIZE; i++){
srand((unsigned)time(NULL));//seed the random number function
n[i] = LB + rand() % (UB - LB + 1 );
}
}
void bubbleSort(int n[]){
int i,temp=0;
for(i=0;i<SIZE-1;i++){
temp=n[i];
n[0]=n[1];
n[i+1]=temp;
}
}
void displayRandomNumbers(int n[], int c[]){
int i;
for(i=0;i<LB;i++)
printf("It is %i\n",n[i]);
pause;
}
If you seed the random number generator with the time, it will keep repeating the same number until the time changes which takes 1 second. Don't do that - seed the generator only once, at the program start.
Only call srand once, at the beginning of the program.
Did you try
srand(time(NULL));
int r = rand();
Consider this code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int ctr;
for(ctr=0;ctr<=10;ctr++)
{
int iSecret;
srand ( time(NULL) );
printf("%d\n",iSecret = rand() % 1000 + 1);
}
}
And it outputs this:
256
256
256
256
256
256
256
256
256
256
Unfortunately, I want the output to print 10 different random numbers in that loop.
Move the call to srand(time(NULL)); to before the for loop.
The problem is that time() changes only once in every second, but you're generating 10 numbers, and unless you have an extremely slow CPU, it won't take a second to generate those 10 random numbers.
So you're re-seeding the generator with the same value each time, making it return the same number.
Put srand ( time(NULL) ); before the loop. Your loop probably runs within a second so you are reinitialising the seed with the same value.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int ctr;
srand ( time(NULL) );
for(ctr=0;ctr<=10;ctr++)
{
int iSecret;
printf("%d\n",iSecret = rand() % 1000 + 1);
}
}
keep srand(time(0)) outside of the for loop.
it should not be inside that loop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int ctr;
srand ( time(NULL) );
for(ctr=0;ctr<=10;ctr++)
{
int iSecret;
printf("%d\n",iSecret = rand() % 1000 + 1);
}
}