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.
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.
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;
Make a simple loop to retry the operation if alpha gets 13 :
int alpha = 13;
while (alpha == 13)
alpha = ((rand() % 12) + 1) * 2 + 1;
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
.