samedi 30 septembre 2023

Struggling with fixed rand() output [closed]

I am trying to obtain a random integer value from [1,3]. I have done some research on rand() and srand(), but i can't seem to contain the integer to 1,3. Any simple solutions?

There is a lot of code in between that is not interfering, so this is just a snippet. Assume libraries are present. :)

        int seed;
    printf("Before we start, pick a number to determine how random I will be.\n");  //srand
    scanf("%d", &seed);
        srand(seed);
    printf("All set now!\n");

        int computerPick(){
          int p2select;
          p2select = rand()%(3-1) + 1;    // i need this to return from 1-3
          printf("%d",p2select);
          return p2select;
      }





Aucun commentaire:

Enregistrer un commentaire