0
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(){
        for(int i = 0; i <= 4; i = i + 1) {
                int p1;
                    p1 = fork();
                    wait(NULL);
                    int plato;
                   plato = rand()%2;
                   int cant;
                        cant = rand()%6 + 1;
                        int cant1;
                        cant1 = rand()%4 + 1;
                if (p1 == 0) {
                   if (plato = 0){
                        if (cant > 0){
                           printf("\nMesero %d, PPID=%d, pedido de %d postre/s", (i) ,getppid(), cant);
                        printf (", PID=%d\n", getpid());
                        }
                        exit(0);
              } else if (plato = 1){
                    printf("\nMesero %d, PPID=%d, pedido de %d plato/s de alboniga/s", (i) ,getppid(), cant1);
                        printf (", PID=%d\n", getpid());
                        exit(0);
              }
        } else if (p1<0) {
         printf("ERROR");
        }
}

         return 0;
}

The answer is always the same and I need diferent ones,

I want to generate a random type of "Platos" and if they are desserts or dishes generated a random amount

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
愛mar
  • 3
  • 5

1 Answers1

1

#include <time.h> and as the first statement in main() do srand(time(NULL));. This will create a new sequence of pseudo-random integers as long as you execute your program more than 1 second apart. On Linux /dev/random (blocking) and /dev/urandom (non-blocking) are alternative sources of entropy.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • 1
    "This will create a new sequence of pseudo-random integers each time you run the program." --> as the resolution of `time_t` is seconds, multiple runs in the same second lead to the same sequence. Also use other sources for additional entropy. – chux - Reinstate Monica Nov 16 '22 at 03:59
  • 1
    ..... as long as consecutive runs of the program are far enough apart in time. The standard doesn't specify what the return value represents. Practically, it is *often* an integral type representing number of seconds since midnight, Jan 1, 1970, but that is not always the case (and, even if when is, the precision is often poor, so two calls of `time()` several seconds apart - even in different processes - can give the same return value). – Peter Nov 16 '22 at 04:02
  • @Peter: do you have any evidence of a non-integral type being returned by `time()` on any platform? Do you have any evidence of it not returning values with one second granularity? POSIX requires an integer type and one second granularity, so that rules out many, many systems — and Windows follows POSIX on this point. – Jonathan Leffler Nov 16 '22 at 04:55
  • @JonathanLeffler I'm not aware offhand of any implementations with `time_t` a non-integral type. Some C implementations targeting DOS and OS/2 in the late 80s had different epoch times and processor interrupts to update system time were routinely either disabled or configured with an interval exceeding a second, in order to reduce processor power consumption - so calls of `time()` more than a second apart could return the same value. Although I don't have first-hand knowledge, I've also heard anecdotals about other more recent systems (and libraries for them) having similar behaviours. – Peter Nov 16 '22 at 09:44