简体   繁体   中英

First and Second Call in C

I'm trying to generate a random number between 1 and 6, on every second call a random number between one and six is suppose to be generated and on every other call 6 is suppose to be exported to both pointers, I keep getting the output in the opposite behaviour could someone suggest how to rectify this problem.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void loadedDice(int*, int*);

int main(void)
{
    int a = 0, b = 0;
    srand(time(NULL));
    loadedDice(&a, &b);
    printf("%d %d\n", a, b);
    loadedDice(&a, &b);
    printf("%d %d\n", a, b);
    loadedDice(&a, &b);
    printf("%d %d\n", a, b);

    return 0;

}


void loadedDice(int* export1, int* export2)
{
    if(*export1 == 6 && *export2 == 6)
    {
    *export1 = 1 + (rand()%6);
    *export2 = 1 + (rand()%6);

    } else {
    *export1 = 6;
    *export2 = 6;
    }
}

Change your function to this:

void loadedDice(int* export1, int* export2) {
  if (*export1 == 0 && *export2 == 0) {
    *export1 = 1 + (rand() % 6);
    *export2 = 1 + (rand() % 6);
  } else if (*export1 == 6 && *export2 == 6) {
    *export1 = 1 + (rand() % 6);
    *export2 = 1 + (rand() % 6);

  } else {
    *export1 = 6;
    *export2 = 6;
  }
}

Now, since the initial values are 0, you will produce the random numbers at first call.


Or you could simply initialize your variables to 6, which will not require you to change your function and it is recommended, for readability and maintenance (keeping functions as small as possible).

This method requires minor changes to your code, specifically you should change the initialization in main() to this:

int a = 6, b = 6;

If you don't want to get your variables initialized, then you could use an initializing function, which should assign random values to the variables, without performing any check first (because the values are uninitialized).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void loadedDice(int*, int*);
void init_loadedDice(int*, int*);

int main(void) {
  int a, b;

  srand(time(NULL));
  init_loadedDice(&a, &b);
  printf("%d %d\n", a, b);
  loadedDice(&a, &b);
  printf("%d %d\n", a, b);
  loadedDice(&a, &b);
  printf("%d %d\n", a, b);

  return 0;

}

void init_loadedDice(int* export1, int* export2) {
  *export1 = 1 + (rand() % 6);
  *export2 = 1 + (rand() % 6);
}

void loadedDice(int* export1, int* export2) {
  if (*export1 == 6 && *export2 == 6) {
    *export1 = 1 + (rand() % 6);
    *export2 = 1 + (rand() % 6);

  } else {
    *export1 = 6;
    *export2 = 6;
  }
}

If you really don't want to use a second function (which is recommended) and don't want to initialize your variables in main, you could use a flag for the function (but I don't see why doing it this way):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void loadedDice(int*, int*, int*);

int main(void) {
  // flag, named 'uninit' will be one
  // as long as 'a' and 'b' are uninitialized
  int a, b, uninit = 1;

  srand(time(NULL));
  loadedDice(&a, &b, &uninit);
  printf("%d %d\n", a, b);
  loadedDice(&a, &b, &uninit);
  printf("%d %d\n", a, b);
  loadedDice(&a, &b, &uninit);
  printf("%d %d\n", a, b);

  return 0;

}

void loadedDice(int* export1, int* export2, int* uninit) {
  if(*uninit) {
    *export1 = 1 + (rand() % 6);
    *export2 = 1 + (rand() % 6);
    *uninit = 0; // change the value of the flag
  } else if (*export1 == 6 && *export2 == 6) {
    *export1 = 1 + (rand() % 6);
    *export2 = 1 + (rand() % 6);

  } else {
    *export1 = 6;
    *export2 = 6;
  }
}

只需将ab初始化为6而不是0

    int a = 6, b = 6;

Another method based on G. Samaras answer:

void loadedDice(int* export1, int* export2) {
    static int check = 1;
  if (check == 1) {
    *export1 = 1 + (rand() % 6);
    *export2 = 1 + (rand() % 6);
    check = 0;
  } else if (*export1 == 6 && *export2 == 6) {
    *export1 = 1 + (rand() % 6);
    *export2 = 1 + (rand() % 6);   
  } else {
    *export1 = 6;
    *export2 = 6;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM