c# - 我的随机数总是给我 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 产生。有任何想法吗?
解决方案
有多个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 毫秒内完成。
推荐阅读
- json - 如何创建自定义表单验证器以仅接受 Angular 中的有效 JSON
- angular - Ng-Chart ocultar 数据集
- java - 验证后 Spring REST 更新参数值
- php - 将 preg_replace 转换为 preg_replace_callback
- angular - Html2Canvas 呈现完整的 div 内容
- swift - calendar.date(bySetting: .day, value: 1, of: Date()) 错误地总是加一个月
- python - PyQt QTableView 获取跨多列的单元格的起始索引?
- asciidoc - 从注释代码生成结构化文档
- python - 对多维 numpy 数组进行排序
- python - 监听简单表上的插入/更新事件