generate -,+ and * randomly
Hi i would like to generate -,+ and * as char randomly. Please can some1 advice me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(0));
while(1)
{
int random_integer = rand()%3;
char p;
switch(random_integer)
{
case 0: p='-';break;
case 1: p='+';break;
case 2: p='*';break;
}
cout << p;
cin.get();
}
}
|
thanks
You might also want to consider having them in an array and randomly shuffling the array, this is less code and lookers cleaner
The common technique is called a 'look-up table', or LUT for short:
1 2 3
|
static const char options[] = "-+*";
char random_option = options[ rand() % 3 ];
cout << random_option;
|
Topic archived. No new replies allowed.