Reputation: 115
I am trying to create a method that generates a random number from a specific selection of numbers, it must be either 0, 90, 180 or 270.
This is the method I have so far to generate a random number between a certain range, but I am not sure how to generate a random number from a specific selection.
int randomNum(int min, int max) {
return rand() % max + min;
}
Upvotes: 1
Views: 695
Reputation: 118
Umm, So as I think you're trying to choose a specific number in a array or so called a list of options, I've a quick example for you here. You can just create a array with all your options and make this method to return a value from the given array.
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main ()
{
srand ( time(NULL) ); //initialize the random seed
const char arrayNum[4] = {'1', '3', '7', '9'};
int RandIndex = rand() % 4; //generates a random number between 0 and 3
cout << arrayNum[RandIndex];
}
More details - http://www.cplusplus.com/forum/beginner/26611/
Upvotes: 2