•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,705 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,203 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 379 | Replies: 4 | Solved
![]() |
•
•
Join Date: Nov 2007
Posts: 73
Reputation:
Rep Power: 1
Solved Threads: 0
i'm trying to get random numbers in a loop like this
the problem is that i keep getting the same number
syntax Syntax (Toggle Plain Text)
#include <stdio.h> #include <time.h> int getRand ( int a , int b ) { return a+rand()%(b-a+1); } int main() { srand ( time( NULL ) ) ; int i; for(i=0 ; i<6 ; i++) printf("%d\n",getRand(0,9)); return 0; }
the problem is that i keep getting the same number
The same number or some numbers are being repeated? Your code works just fine when Edward tests it, but repeated numbers are expected unless you go out of your way to avoid them with something like a random shuffle:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int *values;
static int index;
int initRandUnique(int lo, int hi)
{
values = malloc((hi - lo) * sizeof *values);
if (values == NULL)
return 0;
{
int limit = hi - lo;
int i;
for (i = 0; i < limit; ++i)
values[i] = lo++;
for (i = 0; i < limit - 1; ++i) {
int r = i + rand() % (limit - i);
int temp = values[r];
values[r] = values[i];
values[i] = temp;
}
}
return 1;
}
int getRandUnique()
{
return values[index++];
}
int main()
{
int i;
srand(time(NULL));
initRandUnique(0, 9);
for (i = 0; i < 6; ++i)
printf("%d\n", getRandUnique());
return 0;
} If at first you don't succeed, keep on sucking until you do succeed.
•
•
Join Date: Nov 2007
Posts: 73
Reputation:
Rep Power: 1
Solved Threads: 0
ok.
i wanted to use something simple cause my actual program is quite difficult
i need to do something like this:
at this point it gives me the same random number.
i wanted to use something simple cause my actual program is quite difficult
i need to do something like this:
syntax Syntax (Toggle Plain Text)
#include <stdio.h> #include <time.h> #include <stdlib.h> int sig,pid; int getRand ( int a , int b ) { return a+rand()%(b-a+1); } int main() { pid = getpid(); srand ( time( NULL ) ) ; int i,n; for(i=0 ; i<6 ; i++) { if(getpid() == pid) sig = fork(); if(sig == 0) { n = getRand(1,9); printf("%d\n",n); sleep(n); exit(0); } return 0; }
at this point it gives me the same random number.
That's just a fancy way of doing this:
You initialize the seed once, and each child process uses the same seed but restarts the sequence from the beginning. In other words, each child process is printing the first number of the sequence, so all of the numbers are going to be the same. You need to reseed rand after spawning a new process:
static unsigned seed;
int getRand(int a, int b)
{
srand(seed);
return a + rand() % (b - a + 1);
}
int main(void)
{
seed = time(NULL);
for (i = 0; i < 10; ++i)
printf("%d\n", getRand(1, 9));
return 0;
}#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int sig,pid;
int getRand ( int a , int b )
{
return a+rand()%(b-a+1);
}
int main()
{
pid = getpid();
int i,n;
for(i=0 ; i<6 ; i++)
{
if(getpid() == pid)
sig = fork();
if(sig == 0)
{
srand ( time( NULL ) );
n = getRand(1,9);
printf("%d\n",n);
sleep(n);
exit(0);
}
}
return 0;
} If at first you don't succeed, keep on sucking until you do succeed.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- Random number generator's (C++)
- Need to know how to create a database that will generate a random number (Database Design)
- Random number generation (C)
- Help with random number gen (C++)
Other Threads in the C Forum
- Previous Thread: Need help on my assignment !!! - about file streams :((
- Next Thread: about tabs


Linear Mode