-1

Here is my code:

alpha = ((rand() % 12) + 1) * 2 + 1;

I want to generate random odd numbers between 0-25. But except integer 13. How can I fix this? Thank you.

Lehue
  • 425
  • 8
  • 26
misshyde
  • 45
  • 1
  • 8

3 Answers3

1

Generates number from 0 to 23. If it's a 13, then store 25 in your variable :

alpha = ((rand() % 11) + 1) * 2 + 1;
if (alpha == 13) alpha = 25;
K.Hacene
  • 125
  • 4
0

Make a simple loop to retry the operation if alpha gets 13 :

  int alpha = 13;
  while (alpha == 13)
    alpha = ((rand() % 12) + 1) * 2 + 1;
user3794667384
  • 437
  • 7
  • 23
0
int universe[] = {1, 3, 5, 7, /* ... omit 13 ... */ 25};
int index = randto(sizeof universe / sizeof *universe);
alpha = universe[index];

Where randto(n) returns a random number from 0 up to and excluding n.

pmg
  • 106,608
  • 13
  • 126
  • 198