[英]C# Slotmachine winning algorythm
我正在尝试对老虎机进行编程。
我有5个滚筒和7张获奖照片。 看起来像
-----|-----|-----|-----|----|
-----|-----|-----|-----|----|
-----|-----|-----|-----|----|
我得到每卷3张图片。
foreach (var s in Walze1)
{
Random rnd = new Random();
int perCent = rnd.Next(Walze1dot7, 110);
//10 % Chance auf 7 per Walze
if (perCent <= 10)
{
Walze1dot7 = 11;
s.Text = "7";
}
//10 % Chance auf Glocke per Walze
else if (perCent <= 20 & perCent > 10)
{
s.Text = "Glocke";
}
//15 % Chance auf Melone
else if (perCent <= 35 & perCent > 20)
{
s.Text = "Melone";
}
//15 % Chance auf Pflaume
else if (perCent <= 35 & perCent > 50)
{
s.Text = "Pflaume";
}
//20 % Chance auf Orange
else if (perCent <= 70 & perCent > 50)
{
s.Text = "Orange";
}
//20 % Chance auf Zitrone
else if (perCent <= 90 & perCent > 70)
{
s.Text = "Zitrone";
}
//20 % Chance auf Kirche
else if (perCent <= 110 & perCent > 90)
{
s.Text = "Kirche";
}
}
一个滚筒上只能有一个7。
我的问题:
我赢了太多……随机代码经常给我带来成功的画面(一行至少要有3张相同的画面)。
所以,我怎么可以改变我的代码,我就会更经常松动 ?
编辑
它不是重复的,因为我知道如何生成一个随机数...我需要的是我不经常获得获奖图片(每行3张)...
假设我在每次点击2欧元时使用100欧元。 经过50次点击,我得到的总金额超过300欧元。
我没有对其进行测试,但是我建议您可以执行以下操作:
使用System.Collections.Generic添加; 到您的文件。
现在,procents是真正的procents,对语句进行了优化,Random初始化不在循环中,并且您存储了滚动的内容。
//sorry for the bad performance, i didn't optimize it.
//you could do that by making a list with the strings in it and remove the one you rolled.
List<string> rolled = new List<string>();
Random rnd = new Random();
foreach (var s in Walze1)
{
again:
//generate an number with min 0, max 99
int perCent = rnd.Next(Walze1dot7, 100);
//10 % Chance, 0 to 9
if (perCent < 10)
{
s.Text = "7";
}
//10 % Chance, 10 to 19
else if (perCent < 20)
{
s.Text = "Glocke";
}
//10 % Chance, 20 to 29
else if (perCent < 30)
{
s.Text = "Melone";
}
//15 % Chance, 30 to 44
else if (perCent < 45)
{
s.Text = "Pflaume";
}
//15 % Chance, 45 to 59
else if (perCent < 60)
{
s.Text = "Orange";
}
//20 % Chance, 60 to 79
else if (perCent < 80)
{
s.Text = "Zitrone";
}
//20 % Chance, 80 to 99
else if (perCent < 100)
{
s.Text = "Kirche";
}
if(rolled.Contains(s.Text)){goto Again;}
rolled.Add(s.Text);
}
在if语句中使用(按位与)&而不是(逻辑与)&&是一个问题,但这不是主要问题。 您也不应该每次调用此函数时都生成一个新的Random()-这是一项昂贵的操作,经常运行它会降低您的速度。
您说您已经测试过了,但是没有测试随机性。 当您生成随机事件时,应编写一个测试用例,以查看它们实际是多么随机。 在您的特定情况下,您可以创建一个Dictionay<string,int>
并将其初始化为每个可以随机选择的字符串,然后在每次生成该字符串时对其加1。
static Dictionary<string,int> CountUses = new Dictionary<string, int>();
/* do this once - so they always print in order*/
CountUses.Add("", 0);
CountUses.Add("7", 0);
CountUses.Add("Glocke", 0);
CountUses.Add("Melone", 0);
CountUses.Add("Pflaume", 0);
CountUses.Add("Orange", 0);
CountUses.Add("Zitrone", 0);
CountUses.Add("Kirche", 0);
/* use this in your function */
if (!CountUses.ContainsKey(s.Text))
CountUses.Add(s.Text, 0);
CountUses[s.Text]++;
然后调用您的函数10,000,000次,看看数字是什么样子-它们应该相当接近。
for ( int x = 1; x < 10000000; ++x)
tester(); //function I made to call your code
foreach (var key in CountUses.Keys)
Console.WriteLine(String.Format("{0,10} uses of {1,8} = {2:P}", CountUses[key], key, CountUses[key]/ 10000000.0f));
对于您的代码,结果分布不均,有时为空:
1376943 uses of = 13.77%
998104 uses of 7 = 9.98%
937056 uses of Glocke = 9.37%
1358398 uses of Melone = 13.58%
0 uses of Pflaume = 0.00%
1814880 uses of Orange = 18.15%
1788699 uses of Zitrone = 17.89%
1725919 uses of Kirche = 17.26%
用&&替换&是不同的,但仍然不正确(足够接近以产生随机差异):
1358963 uses of = 13.59%
1013747 uses of 7 = 10.14%
891739 uses of Glocke = 8.92%
1382211 uses of Melone = 13.82%
0 uses of Pflaume = 0.00%
1818295 uses of Orange = 18.18%
1825153 uses of Zitrone = 18.25%
1709891 uses of Kirche = 17.10%
使用测试代码,可以很容易地找到最大的错误,然后查看所获得的百分比是否确实是您所期望的百分比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.