C# 我的随机数总是给我4,而我不';我不知道为什么

C# 我的随机数总是给我4,而我不';我不知道为什么,c#,C#,首先,这是代码。随机声明在第3行,随机使用在第15行左右 public static void wildPokemonEncounter(string pokemonRegion) { Pokemon wildPokemonEncountered = null; Random rnd = new Random(); int possiblePokemon = 0; foreach (KeyValuePair<string, pokemonStats>

首先,这是代码。随机声明在第3行,随机使用在第15行左右

public static void wildPokemonEncounter(string pokemonRegion)
{
    Pokemon wildPokemonEncountered = null;
    Random rnd = new Random();
    int possiblePokemon = 0;

    foreach (KeyValuePair<string, pokemonStats> kvp in GameReference.pokemonInformation)
    {
        if (kvp.Key != "Bublbasuar" && kvp.Key != "Squirtle" && kvp.Key != "Charmander")
        {
            if (pokemonRegion == "Plain")
            {

                if ((kvp.Value.typeOfPokemon == "Plant" || kvp.Value.typeOfPokemon == "Normal" || kvp.Value.typeOfPokemon == "Bug") && kvp.Value.evolutionNumber==1)
                {
                    possiblePokemon += 1;
                }
            }
        }
    }
    int whichPokemon = rnd.Next(1, possiblePokemon);
    int i = 1;
    Console.WriteLine(possiblePokemon+" "+ i + " " + whichPokemon);
    foreach (KeyValuePair<string, pokemonStats> kvp in GameReference.pokemonInformation)
    {
        if (kvp.Key != "Bulbasuar" && kvp.Key != "Squirtle" && kvp.Key != "Charmander")
        {
            if (pokemonRegion == "Plain")
            {
                pokemonStats thisPokemonsStats = GameReference.pokemonInformation[kvp.Key];
                if ((thisPokemonsStats.typeOfPokemon == "Plant" || thisPokemonsStats.typeOfPokemon == "Normal" || thisPokemonsStats.typeOfPokemon == "Bug") && thisPokemonsStats.evolutionNumber == 1)
                {
                    if (i == whichPokemon)
                    {
                        wildPokemonEncountered = new Pokemon(kvp.Key, kvp.Value);
                        slowTyper("`");
                        slowTyper("You found a Pokemon! Its a " + wildPokemonEncountered.name + "! Its level " + wildPokemonEncountered.level + " and of the type " + wildPokemonEncountered.typeOfPokemon + ".~");
                        Battle B = new Battle();
                        slowTyper("You enter into battle with the opposing Pokemon.");
                        Pokemon your_active_pokemon = null;
                        foreach (Pokemon pok in GameReference.pokemonInBag)
                        {
                            if (pok.is_Starting_Pokemon == true)
                            {
                                your_active_pokemon = pok;
                            }
                        }
                        B.PokemonBattle(wildPokemonEncountered, your_active_pokemon);
                        break;
                    }
                    else
                    {
                        i += 1;
                    }
                }
            }
        }
    }
}
publicstaticvoidwildpokemonecounter(字符串pokemonRegion)
{
口袋妖怪WildPokemoneCountered=null;
随机rnd=新随机();
int-possiblepokeon=0;
foreach(GameReference.pokemoninInformation中的KeyValuePair kvp)
{
if(kvp.Key!=“Bublbasuar”和&kvp.Key!=“Squirtle”和&kvp.Key!=“Charmander”)
{
如果(口袋妖怪区域==“普通”)
{
如果((kvp.Value.typeOfPokemon==“Plant”| | kvp.Value.typeOfPokemon==“Normal”| | kvp.Value.typeOfPokemon==“Bug”)&&kvp.Value.evolutionNumber==1)
{
可能参数+=1;
}
}
}
}
int whichPokemon=rnd.Next(1,可能的密码);
int i=1;
Console.WriteLine(可能是epokeon+“”+i+“”+whichPokemon);
foreach(GameReference.pokemoninInformation中的KeyValuePair kvp)
{
if(kvp.Key!=“Bulbasuar”和&kvp.Key!=“Squirtle”和&kvp.Key!=“Charmander”)
{
如果(口袋妖怪区域==“普通”)
{
pokemonStats thispokemonstats=GameReference.pokemonInformation[kvp.Key];
如果((thispokemonstats.typeOfPokemon==“Plant”| | thispokemonstats.typeOfPokemon==“Normal”| | | thispokemonstats.typeOfPokemon==“Bug”)&&thispokemonstats.evolutionNumber==1)
{
if(i==whichPokemon)
{
WildPokemoneCountered=新的口袋妖怪(kvp.Key,kvp.Value);
慢打字机(“`”);
slowTyper(“你找到了一个口袋妖怪!它是“+WildPokemoneCountered.name+”!它的级别“+WildPokemoneCountered.level+”和类型“+WildPokemoneCountered.typeOfPokemon+”);
战斗B=新的战斗();
slowTyper(“你进入了与对手口袋妖怪的战斗。”);
口袋妖怪your\u active\u Pokemon=null;
foreach(游戏参考中的口袋妖怪pok.pokemonibag)
{
if(pok.is_start_Pokemon==true)
{
你的_active_pokemon=pok;
}
}
B.口袋妖怪(野生口袋妖怪,你的活跃口袋妖怪);
打破
}
其他的
{
i+=1;
}
}
}
}
}
}

我测试了一个while-true循环,以检查出于某种原因,随机变量是否在代码的这个区域不起作用,但它起作用了。然而,出于某种原因,每次我运行它时,我都会得到一个4,导致一只Rattata产卵。有什么想法吗?

有多个stackoverflow源,提到创建一个
新的Random()
的时间太近,导致几乎相同的种子(我无法从文档中验证这一点)

其他人说,如果一毫秒过去了,你就没事了:

实际上,我能想到的唯一能解决你问题的方法就是使用一个静态随机变量,如下所示:

private static Random rnd = new Random();

public static void wildPokemonEncounter(string pokemonRegion)
{
    Pokemon wildPokemonEncountered = null;

    int possiblePokemon = 0;

    foreach (KeyValuePair<string, pokemonStats> kvp in GameReference.pokemonInformation)
    {
        if (kvp.Key != "Bublbasuar" && kvp.Key != "Squirtle" && kvp.Key != "Charmander")
        {
            if (pokemonRegion == "Plain")
            {

                if ((kvp.Value.typeOfPokemon == "Plant" || kvp.Value.typeOfPokemon == "Normal" || kvp.Value.typeOfPokemon == "Bug") && kvp.Value.evolutionNumber==1)
                {
                    possiblePokemon += 1;
                }
            }
        }
    }
    int whichPokemon = rnd.Next(1, possiblePokemon);
    int i = 1;
    Console.WriteLine(possiblePokemon+" "+ i + " " + whichPokemon);
    foreach (KeyValuePair<string, pokemonStats> kvp in GameReference.pokemonInformation)
    {
        if (kvp.Key != "Bulbasuar" && kvp.Key != "Squirtle" && kvp.Key != "Charmander")
        {
            if (pokemonRegion == "Plain")
            {
                pokemonStats thisPokemonsStats = GameReference.pokemonInformation[kvp.Key];
                if ((thisPokemonsStats.typeOfPokemon == "Plant" || thisPokemonsStats.typeOfPokemon == "Normal" || thisPokemonsStats.typeOfPokemon == "Bug") && thisPokemonsStats.evolutionNumber == 1)
                {
                    if (i == whichPokemon)
                    {
                        wildPokemonEncountered = new Pokemon(kvp.Key, kvp.Value);
                        slowTyper("`");
                        slowTyper("You found a Pokemon! Its a " + wildPokemonEncountered.name + "! Its level " + wildPokemonEncountered.level + " and of the type " + wildPokemonEncountered.typeOfPokemon + ".~");
                        Battle B = new Battle();
                        slowTyper("You enter into battle with the opposing Pokemon.");
                        Pokemon your_active_pokemon = null;
                        foreach (Pokemon pok in GameReference.pokemonInBag)
                        {
                            if (pok.is_Starting_Pokemon == true)
                            {
                                your_active_pokemon = pok;
                            }
                        }
                        B.PokemonBattle(wildPokemonEncountered, your_active_pokemon);
                        break;
                    }
                    else
                    {
                        i += 1;
                    }
                }
            }
        }
    }
}
private static Random rnd=new Random();
公共静态void wildpokemoneccounter(字符串pokemonRegion)
{
口袋妖怪WildPokemoneCountered=null;
int-possiblepokeon=0;
foreach(GameReference.pokemoninInformation中的KeyValuePair kvp)
{
if(kvp.Key!=“Bublbasuar”和&kvp.Key!=“Squirtle”和&kvp.Key!=“Charmander”)
{
如果(口袋妖怪区域==“普通”)
{
如果((kvp.Value.typeOfPokemon==“Plant”| | kvp.Value.typeOfPokemon==“Normal”| | kvp.Value.typeOfPokemon==“Bug”)&&kvp.Value.evolutionNumber==1)
{
可能参数+=1;
}
}
}
}
int whichPokemon=rnd.Next(1,可能的密码);
int i=1;
Console.WriteLine(可能是epokeon+“”+i+“”+whichPokemon);
foreach(GameReference.pokemoninInformation中的KeyValuePair kvp)
{
if(kvp.Key!=“Bulbasuar”和&kvp.Key!=“Squirtle”和&kvp.Key!=“Charmander”)
{
如果(口袋妖怪区域==“普通”)
{
pokemonStats thispokemonstats=GameReference.pokemonInformation[kvp.Key];
如果((thispokemonstats.typeOfPokemon==“Plant”| | thispokemonstats.typeOfPokemon==“Normal”| | | thispokemonstats.typeOfPokemon==“Bug”)&&thispokemonstats.evolutionNumber==1)
{
if(i==whichPokemon)
{
WildPokemoneCountered=新的口袋妖怪(kvp.Key,kvp.Value);
慢打字机(“`”);
slowTyper(“你找到了一个口袋妖怪!它是“+WildPokemoneCountered.name+”!它的级别“+WildPokemoneCountered.level+”和类型“+WildPokemoneCountered.typeOfPokemon+”);
战斗B=新的战斗();
slowTyper(“你进入了与对手口袋妖怪的战斗。”);
口袋妖怪your\u active\u Pokemon=null;
foreach(游戏参考中的口袋妖怪pok.pokemonibag)
{
if(pok.is_start_Pokemon==true)
{
你的_active_pokemon=pok;
}
}
B.口袋妖怪(野生口袋妖怪,你的活跃口袋妖怪);
打破
}
其他的
{
i+=1;
}
}
}
}
}
}
希望有人能做到
using System;
using System.Collections.Generic;

public class Program
{
    private static HashSet<int> randomValues = new HashSet<int>();

    public static void Main()
    {
        for(int i = 0; i < 2000; i++)
        {
            wildPokemonEncounter();
        }

        Console.WriteLine("1st iteration results");
        foreach(int rnd in randomValues) {
            Console.WriteLine(rnd);
        }

        randomValues.Clear();

        for(int i = 0; i < 20000; i++)
        {
            wildPokemonEncounter();
        }

        Console.WriteLine("2nd iteration results");
        foreach(int rnd in randomValues) {
            Console.WriteLine(rnd);
        }
    }

    //static Random rnd = new Random();

    public static void wildPokemonEncounter()
    {
        int possiblePokemon = 4;
        Random rnd = new Random();
        int whichPokemon = rnd.Next(1, possiblePokemon);
        if(!randomValues.Contains(whichPokemon)) {
            randomValues.Add(whichPokemon);
        }


    }
}