Reputation: 1757
I am making a roulette table and wanted to ask a question, but also have help with fixing code.
1, currently I am using rand()%37 as my random number generator for the 0-36 numbers. Is there a better generator that gives a more random number?
2, I am unsure how to check values in an array against an if statement. (check code below)
int red[18] = {1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36};
int main ()
{
srand(time(NULL));
r = rand() % 3;
cout << r << endl;
for(int i = 0; i<18; i++)
if (r==red[i])
{
cout << "hello" << endl;
break;
}
else
{
cout << "bye" << endl;
break;
}
return 0;
}
When i run this code, it returns bye even if r (random number) is in the array red.
To woolstar: this is a simplfied version of my actual code. I want to have a random number be generated (r) and then check this number against the array red, if r is in the array I want it to be considered true and print hello in this case
E.g. r = 14, is 14 in array red? Yes: print hello
Edit: Here is my actual code, when running the random number and find in case B and C it comes back with errors saying: initialization are skipping case labels
// Random number generator
random_device rd;
mt19937 gen (rd());
uniform_int_distribution<>
dis (0, 36);
switch (play_method)
{
case 1: // Color Play
cout << "A - Play Red.\n";
cout << "B - Play Black.\n";
cout << "C - Play Green.\n";
cout << "Which color will you bet on?" << endl;
cin >> color;
switch (color)
{
case 'A': // Red Play
int rred = dis(gen);
auto
it = find(begin(red),end(red), rred);
if(it!=end(red))
{
win_amt = bet_amt*2;
total_amt = total_amt + (win_amt - bet_amt);
cout << "Congratulations you rolled a " << r << " and have won: $" << win_amt << "!" << endl;
cout << "You now have: $" << total_amt << " remaining." << endl;
}
else
{
total_amt = total_amt - bet_amt;
cout << "Unlucky, you rolled a " << r << " and have have lost $" << bet_amt << endl;
cout << "You have $ " << total_amt << " remaining." << endl;
} // Red play if/else end
break; // Break for case A: Red play
case 'B': // Black play
int rblack = dis(gen);
auto
it1 = find(begin(black),end(black), rblack);
if(it1!=end(black))
{
win_amt = bet_amt*2;
total_amt = total_amt + (win_amt - bet_amt);
cout << "Congratulations you rolled a " << r << " and have won: $" << win_amt << "!" << endl;
cout << "You now have: $" << total_amt << " remaining." << endl;
}
else
{
total_amt = total_amt - bet_amt;
cout << "Unlucky, you rolled a " << r << " and have have lost $" << bet_amt << endl;
cout << "You have $ " << total_amt << " remaining." << endl;
} // Black play if/else end
break; // Break for case B: Black play
case 'C':
int rgreen = dis(gen);
auto
it2 = find(begin(green),end(green), rgreen);
if(it1!=end(green))
{
win_amt = bet_amt*37;
total_amt = total_amt + (win_amt - bet_amt);
cout << "Congratulations you rolled a " << r << " and have won: $" << win_amt << "!" << endl;
cout << "You now have: $" << total_amt << " remaining." << endl;
}
else
{
total_amt = total_amt - bet_amt;
cout << "Unlucky, you rolled a " << r << " and have have lost $" << bet_amt << endl;
cout << "You have $ " << total_amt << " remaining." << endl;
} // Green play if/else end */
break;
} // Switch color end
break; // break for case 1: Color play
Upvotes: 0
Views: 3853
Reputation: 16148
1, currently I am using rand()%37 as my random number generator for the 0-36 numbers. Is there a better generator that gives a more random number?
Yes absolutely. Please consider using the C++11 random number facilities (or Boost.Random should you not have C++11).
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis { 0, 36 };
//print random number between 0 and 36
std::cout << dis(gen);
Please see the following reference:
http://en.cppreference.com/w/cpp/numeric/random
2, I am unsure how to check values in an array against an if statement. (check code below)
I would use std::find
#include <random>
#include <iostream>
#include <algorithm>
int red[18] = {1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36};
int main () {
std::random_device rd;
std::mt19937 gen { rd() };
std::uniform_int_distribution<> dis { 0, 36 };
int r=dis(gen);
auto it=std::find(std::begin(red), std::end(red), r);
if(it!=std::end(red)) {
std::cout << "hello\n";
}
else {
std::cout << "hello\n";
}
return 0;
}
Please see the following reference:
http://en.cppreference.com/w/cpp/algorithm/find
Hopefully this will be compelling enough to ditch rand()
.
http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Upvotes: 3
Reputation: 516
As I believe its a red herring to the main problem, I will ignore (if that is what this comment is) the line
r = rand() % 3;
If r is 1, hello will be printed. otherwise the else is executed first. This prints bye and breaks the loop.
The for can never run past one loop iteration, due the break statement in both clauses of the if - its easier to see if you write the loop this way (I strongly suggest you get into the habit of using parentheses more often)
for(int i = 0; i<18; i++)
{
if (r==red[i])
{
cout << "hello" << endl;
}
else
{
cout << "bye" << endl;
}
break;
}
return 0;
which essentially does this
for(int i = 0; i<18; i++)
{
// do something not affecting flow control
break;
}
return 0;
which is
int i =0;
// do something not affecting flow control
I am guessing this is not what you wanted and suggest a flag may be the best way, leaving the printing to after the loop has finished.
Upvotes: 2