fork and rand()


 
Thread Tools Search this Thread
Top Forums Programming fork and rand()
# 1  
Old 12-14-2009
fork and rand()

Hi,
I want build 10 processus with fork and that each processus write a value betwen 0 and 9 , but each processus send the same value .

So my code ,you can compile and try .
Code:
#include <time.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/ipc.h> 
#include <sys/shm.h> 
#include <sys/sem.h> 
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>

int pid;
main()
{
    srand(time(NULL));
    int i=10;
    while(i>0)
    {
        pid=fork();
        if(pid==0) 
        {
            //srand(time(NULL));
            printf("\n %d ",rand()%10);
            i=-1;
        }
        i--;
    }
exit(0); 
}

Can you tell me how use rand() corectly?
# 2  
Old 12-14-2009
Simple problem, simple solution. You're initializing the PRNG once, and the state of it is copied into each child, giving the same value in each process. Move the initialization into the child, and everything should work as expected.
# 3  
Old 12-14-2009
I am ok with you moreaver is more logic
but i have already try to move srand(time(NULL)) in child .

Code:
int pid;
main()
{
   // srand(time(NULL));
    int i=10;
    while(i>0)
    {
        pid=fork();
        if(pid==0) 
        {
            srand(time(NULL));
            printf("\n %d ",rand()%10);
            i=-1;
        }
        i--;
    }
exit(0); 
}


On my solaris i have
7
7
7
7
...
# 4  
Old 12-14-2009
I'm sorry, my bad. I really should think code through.

Moving the srand() into the child is the first step. The second is to find some way to better initialize it. srand() initializes the PRNG based on the seed value passed, in this case the seconds since the epoch. And unless you're using a computer from way before 1980 (which is very doubtful), you'll spawn the 9 processes in under 1 second.

You could try initializing it using a few bytes from /dev/random, if it's available on the system, or by sleep()ing for a second between each call.
# 5  
Old 12-14-2009
...or just reading bytes from /dev/random instead of using rand() at all.
# 6  
Old 12-14-2009
Good idea with sleep this turn on.
But I don't understand the answer of Corona.

Does it exits a method for initialize srand with µs ?
# 7  
Old 12-14-2009
Quote:
Originally Posted by carton99
But I don't understand the answer of Corona.
Bytes read from /dev/random are random. Having read them, you already have a random number.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

fork()

I'm trying to run a simple test on how to use fork(), i'm able to execute the child process first then the parent, but how can I execute parent then child..? Thanks! (1 Reply)
Discussion started by: l flipboi l
1 Replies

2. Programming

Fork()

does fork() spawn only the parent process, what if fork() is looped, does it spawn the parent and the child? (4 Replies)
Discussion started by: Peevish
4 Replies

3. Programming

Fork and \n

Hi, I wrote a simple program for understanding the fork command. The code is as below int main(void) { fork(); printf("hi 1 \n"); fork(); printf("hi 2 \n"); fork(); printf("hi 3 \n"); } I am getting a variation in the number of times the printf is called if i remove the \n from each... (2 Replies)
Discussion started by: xyz123456
2 Replies

4. UNIX for Advanced & Expert Users

Fork and \n

Hi, I wrote a simple program for understanding the fork command. The code is as below int main(void) { fork(); printf("hi 1 \n"); fork(); printf("hi 2 \n"); fork(); printf("hi 3 \n"); } I am getting a variation in the number of times the printf is called if i remove the \n from each of... (1 Reply)
Discussion started by: xyz123456
1 Replies

5. Programming

Fork or what?

Hello all. I'm developing a filetransfer application, which is supposed to work sort of like dcc, with multiple transfers etc. Now i wonder what the best way to manage the transfers is. Should i fork() for each new transfer, hogging loads of memory or use pthreads? Maybe I can use select to see... (0 Replies)
Discussion started by: crippe
0 Replies

6. Programming

fork()

#include <stdio.h> #include <string.h> #include <sys/types.h> #define MAX_COUNT 200 #define BUF_SIZE 100 void main(void) { pid_t pid; int i; char buf; fork(); pid = getpid(); for (i = 1; i <= MAX_COUNT; i++) { sprintf(buf,... (2 Replies)
Discussion started by: MKSRaja
2 Replies

7. Programming

rand()

Hi all, Is the rand() function in C uniform or normal distribution. If it is unform, is there a random function that is normal. Thanks and Regards (2 Replies)
Discussion started by: omran
2 Replies

8. Programming

fork() fd

I run this code, actually I want to both processes print the message from "data". But only one does. What happens? Anyone can help? #include <stdio.h> main(){ int fd, pid; char x; fd = open("data",0); /* open file "data" */ pid = fork(); if(pid != 0){ wait(0); ... (2 Replies)
Discussion started by: Herman
2 Replies

9. UNIX for Dummies Questions & Answers

Fork

What is a fork? Why would one create a fork? What are the advantages and disadvantages of using a fork? Please advise. Thank You. Deepali (5 Replies)
Discussion started by: Deepali
5 Replies
Login or Register to Ask a Question