首页 > 解决方案 > 我的随机数总是给我 4 我不知道为什么

问题描述

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

我已经测试了只是放入一个真正的循环,以检查随机是否由于某种原因在代码的这个区域中不起作用但它起作用了。但是,出于某种原因,每次我运行它时,我都会得到一个 4 导致 Rattata 产生。有任何想法吗?

标签: c#

解决方案


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

随机数发生器只产生一个随机数

其他人说,如果过了一毫秒,你就没事了:new 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

根据这里的小提琴,结果两个答案都是正确的

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 毫秒内完成。


推荐阅读