C#-带种子的随机数 [英] C# - Random number with seed

查看:739
本文介绍了C#-带种子的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

var rand = new Random(0);
for(int i = 0; i < 100; i++)
{
  Console.WriteLine(rand.Next(0, 100));
}

程序应该给我100倍的相同数字(因为种子相同),但是它给出了不同的数字...
为什么?

And program should give me 100 times the same number (because seed is the same), but it gives different numbers...
Why?


我什么时候做


When I will do

for(int i = 0; i < 100; i++)
{
  Console.WriteLine(new Random(0).Next);
}

每次返回相同的数字.那意味着种子在变吗?如果是,怎么办?还在增加吗?

That returns the same number every time. That means, seed is changing? If yes, how? Is it increasing?

推荐答案

它不应为您提供100个相同的数字,但每次您重新启动应用程序时都应为您提供完全相同的100个数字.

It should not give you 100 same numbers but it should give you exactly the same 100 numbers each time you restart the app.

种子用于使随机可预测.想象一下,多人游戏中您想要随机的东西.但是您要确保每个玩家/客户的随机行为都相同.而seed是前往此处的方式.

Seed is used to make random predictable. Imagine multiplayer game where you want something to be random. But you want to make sure that this random behaves the same for each player/client. And seed is the way to go here.

这篇关于C#-带种子的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆