这个WP8测验应用程序(由于显而易见的原因而被忽略的问题)不断重复这些问题,好像它一遍又一遍地生成相同的随机数。有一个列表创建功能,可以对问题和生成器本身进行混洗。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
namespace GamingQuiz
{
public partial class GamePage : PhoneApplicationPage
{
public GamePage()
{
InitializeComponent();
ques();
// PotterGay();
}
int MyNumber = 0;
question[] q = new question[101];
public void ques()
{
//questions are here e.g. q[0]=new question("question","a1","a2","a3","a4",correctanswer)
}
// int[] murica = new int[101];
// int MyNumber = 0;
List<int> murica = new List<int>();
public void PotterGay()
{
int hue = 0;
Random a = new Random(100);
while (hue<100){
MyNumber = a.Next(0,100);
if (!murica.Contains(MyNumber))
{
murica.Add(MyNumber);
hue++;
}
}
}
int vCorect = 0;
public void end()
{
intrebare.Visibility = Visibility.Collapsed;
vd.Text = "DICKY DICKY";
var1.Visibility = Visibility.Collapsed;
var2.Visibility = Visibility.Collapsed;
var3.Visibility = Visibility.Collapsed;
var4.Visibility = Visibility.Collapsed;
}
int j = 0;
int fatista = 0;
public void right()
{
fatista = murica[j];
// Random asdf = new Random();
/// j = asdf.Next(100);
intrebare.Text = q[fatista].quest;
var1.Content = q[fatista].v1;
var2.Content = q[fatista].v2;
var3.Content = q[fatista].v3;
var4.Content = q[fatista].v4;
vCorect = q[fatista].gj;
j++;
}
public void start_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
PotterGay();
right();
start.Visibility = Visibility.Collapsed;
}
int scor = 0;
int baba = 0;
private void var_tap(object sender, System.Windows.Input.GestureEventArgs e)
{
{
switch ((string)((Button)sender).Name)
{
case "var1":
if (vCorect == 1)
{
scor++;
vd.Text = "Correct!";
baba++;
}
else
{
scor--;
vd.Text = "Wrong!";
baba++;
}
break;
case "var2":
if (vCorect == 2)
{
scor++;
vd.Text = "Correct!";
baba++;
}
else
{
scor--;
vd.Text = "Wrong!";
baba++;
}
break;
case "var3":
if (vCorect == 3)
{
scor++;
vd.Text = "Correct!";
baba++;
}
else
{
scor--;
vd.Text = "Wrong!";
baba++;
}
break;
case "var4":
if (vCorect == 4)
{
scor++;
vd.Text = "Correct!";
baba++;
}
else
{
scor--;
vd.Text = "Wrong!";
baba++;
}
break;
}
}
if (baba < 101)
{
right();
puncte.Text = Convert.ToString(scor);
}
else
{
murica.Clear();
end();
}
}
}
}
答案 0 :(得分:2)
你的问题在这里
Random a = new Random(100);
那里的100就是所谓的SEED。具有相同种子的Randoms将始终给出相同的数字排列。
你要么想要默认的
Random a = new Random();
或'随机'的种子
Random a = new Random(DateTime.Now.Miliseconds);
引用实际的documentation
为不同的Random对象提供相同的种子值 每个实例产生相同的随机数序列。
如果您的应用程序需要不同的随机数序列,请调用 这个构造函数重复使用不同的种子值。一种方法 产生一个独特的种子价值就是让它与时间有关。
答案 1 :(得分:1)
您应该只调用new Random
一次并继续重复使用随机数生成器。如果你使用相同的种子100
不断更新,那么你获得的数字序列将始终是相同的。
答案 2 :(得分:0)
首先,生成RNG的调用中的100
称为种子,通过指定它,您将获得一系列已知的伪随机数。
其次,从Random a = new Random(100);
方法中移除PotterGay()
行,改为使其成为类成员(private Random a = new Random();
)。你应该只需要一个RNG。即使没有指定的种子,对PotterGay()
方法的快速重复调用最终也会产生相同的随机序列。