如何在游戏中增加概率
how to add probabilities to the game?
这个程序运行得很好,但就像一个真正的赌场老板。我希望在不增加更多可能性的情况下,尽可能减少支出。我想做的方法是给每个数字加上概率。有8种可能性(2,3,4,5,6,7,8,9)我希望7发生得最少。所以我想把概率设定为2-6,75%(各15%),8-9,20%(各10%),7,5%。有人能帮我吗?
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int Random(int low, int high);
int Result(int a, int b, int c, int chips, int bet);
int main()
{
srand( time(0) );
int low1(2), low2(2), low3(2);
int high1(9), high2(9), high3(9);
int menu=0;
int bet =0;
bool quit=0;
cout << "Player's chips: $1000" << endl;
int chips=1000;
while (!quit)
{
cout << "1) Play slot. 2) Exit.";
cin >> menu;
switch (menu)
{
case 1:
{
cout << "Enter your bet: ";
cin >> bet;
if (bet<=0 || bet>chips)
{
cout << "You did not enter a valid bet." << endl;
menu=1;
}
else
{
int a = Random(low1,high1);
int b = Random(low2,high2);
int c = Random(low3,high3);
cout << a << " " << b << " " << c << endl;
chips = Result(a,b,c,chips,bet);
cout << "Player's chips: $" << Result(a,b,c,chips,bet) << endl;
}
break;
}
case 2:
{
cout << "Exiting..." << endl;
quit=1;
break;
}
default:
{
cout << "Not a valid menu !"<<endl;
}
}
}
if (chips <=0)
{
cout << "You have $0 ! Game Over !" << endl;
quit=1;
}
}
int Random(int low, int high)
{
int random;
random = low + rand() % ((high+1)-low);
return random;
}
int Result(int a, int b, int c, int chips, int bet)
{
if ((a==7 && b==7 && c==7))
{
cout << "You won 10 times your bet !($" << (bet*10) << ")" << endl;
chips=chips+(bet*10);
}
if ((a==b==c) && !(a==7 && b==7 && c==7))
{
cout << "You won 5 times your bet !($" << (bet*5) << ")" << endl;
chips=chips+(bet*5);
}
if ((a==b || a==c || b==c) && !(a==b==c) && !(a==7 && b==7 && c==7))
{
chips=chips+(bet*3);
cout << "You won 3 times your bet ! ($" << (bet*3) << ")" << endl;
}
else
{
cout << "You lost your bet ! ($-" << bet << ")" << endl;
chips=chips-bet;
}
return chips;
}
引入一个值数组[0.05、0.15、0.25、0.4、0.55、0.7、0.85],对应于值7、8、9、2、3、4、5。6是如果生成的随机数>0.85。
然后得到一个从0到1的随机数r。
if r <=0.05 then it is 7
else if r <= 0.15 then it is 8
else if r <= 0.1 then it is 9
...
else if r <= 0.85 then it is 5
else it is 6
如果你愿意,你可以想办法把它写成一个循环。
我会如下重写函数(它可以被重写为更好的东西,但这是一个应该有效的开始):
// Generate a random number 2-9, with weighted probabilities
int Random()
{
int number; // number 1-9 to generate
int prob; // probability
// Generate probability value 0-99
prob = rand() % 100;
// Calculate number with desired probability "additive"
if (prob < 15) // 15% probability for 2
number = 2;
else if (prob < 30) // 15% probability for 3
number = 3;
else if (prob < 45) // 15% probability for 4
number = 4;
else if (prob < 60) // 15% probability for 5
number = 5;
else if (prob < 75) // 15% probability for 6
number = 6;
else if (prob < 80) // 5% probability for 7
number = 7;
else if (prob < 90) // 10% probability for 8
number = 8;
else // 10% probability for 9
number = 9;
return number;
}
不要忘记添加以下代码来为随机数生成器种子:
srand (time(NULL));
顺便说一句,我希望这不是一台真正的老虎机,如果是的话,可能会有法律问题:-)
相关文章:
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 有效地使用std::unordered_map来插入或增加键的值
- 如何根据单词在文本中出现的概率输出单词
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- 增加传递地址的值
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 将一对插入地图并增加计数?
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 我的 c++ 应用程序中的运行时间从 0 增加到 60 太快了(例如一毫秒或一微秒)
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 琐碎并发代码的吞吐量不会随着线程数量的增加而增加
- C++:功能在输出前自动使用50减/增加输入数
- 追加位以增加字符的大小
- 如何在游戏中增加概率
- 为什么与组合相比,私有继承会增加有人破坏我的代码的概率