C# 代码不断返回相同的随机数

C# 代码不断返回相同的随机数,c#,windows-phone-8,C#,Windows Phone 8,这个WP8测验应用程序(由于明显的原因而设置的问题)不断重复这些问题,就好像它一次又一次地生成相同的随机数一样。有一个列表创建函数,可以洗牌问题和生成器本身 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation;

这个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();
            }
        }


    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用System.Windows.Media;
命名空间游戏测验
{
公共部分类游戏页面:PhoneApplicationPage
{
公共游戏网页()
{
初始化组件();
ques();
//PotterGay();
}
int MyNumber=0;
问题[]q=新问题[101];
公共行政许可证()
{
//此处有问题,例如q[0]=新问题(“问题”、“a1”、“a2”、“a3”、“a4”,正确答案)
}
//int[]murica=新int[101];
//int MyNumber=0;
List murica=新列表();
公共空间PotterGay()
{ 
int色调=0;
随机a=新随机(100);

而(hue首先,用于生成RNG的调用中的
100
称为种子,通过指定它,您将获得一系列已知的伪随机数

其次,从
PotterGay()
方法中删除
Random a=new Random(100);
行,并将其改为类成员(
private Random a=new Random();
)。您应该只需要一个RNG即可。快速重复调用
PotterGay()
方法可能最终生成相同的随机序列,即使没有指定的种子。

您的问题就在这里

Random a = new Random(100);
其中的100是所谓的种子。具有相同种子的随机数总是给出相同的数字排列

你要么想要默认值

Random a = new Random();
或者是“随机”的种子

Random a = new Random(DateTime.Now.Miliseconds);
引用实际情况

为不同的随机对象提供相同的种子值会导致 每个实例生成相同的随机数序列

如果应用程序需要不同的随机数序列,请调用 此构造函数重复使用不同的种子值。一种方法 产生一种独特的种子价值是使其具有时间依赖性


你应该只调用
new Random
一次,并继续重复使用随机数生成器。如果你继续使用相同的种子
100
更新它,那么你得到的数字序列将始终是相同的。

没有理由对前者进行后一种操作;你正在用更少的熵t执行相同的概念操作韩默认会在那里。是的,这是真的。我把它放在那里是为了完整性'为了解释'种子'的概念。我认为这是因为你发布了一堆代码,几乎没有解释,只是希望人们通读整件事。我也没有看到一个实际的问题。显然,你想解决这个问题lem在标题中提到过,但你在身体的任何地方都不会用短语提问。这是一个很好的观点,但我认为他每次应用程序运行时只给PotterGay打一次电话。除非我完全误解了start_Tap的目的。Tap是新的鼠标点击,速度足够慢,用户随机,可以让你的答案的第二部分不需要的