我的随机数总是给我4,但我不知道为什么

时间:2020-03-03 16:13:33

标签: c#

所以首先是这里的代码。随机声明位于第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;
                    }
                }
            }
        }
    }
}

我已经测试过只放入一会儿true循环,以检查由于某种原因随机变量在此代码区域中是否不起作用,但它是否起作用。但是由于某种原因,我每次运行它时,都会得到四个导致Rattata生成的信息。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

有多个stackoverflow源,其中提到创建new Random()时间太近会导致几乎相同的种子(我无法从文档中对此进行验证)

Random number generator only generating one random number

其他人则说,如果过了毫秒,您就可以了:How much does new Random() depends on time?

实际上,我能想到的唯一可以解决您的问题的方法就是确实使用静态的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;
                    }
                }
            }
        }
    }
}

希望有人可以验证一个或另一个,并为您解释相同的值。

编辑1

根据fiddle here

得出两个答案都是正确的
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);
        }


    }
}

结果:

第一次迭代结果 3 第二次迭代结果 3 1个 2

它是如此之快以至于所有迭代都在1毫秒内完成。

相关问题