简体   繁体   English

如何每天生成一个新的随机数序列?

[英]How to generate a new sequence of random numbers every day?

I'm making a daily challenge in a game. 我在游戏中每天挑战。 Every day should be a new challenge. 每天应该是一个新的挑战。 My procedural generation uses a random number generator . 我的程序生成使用随机数生成器 Attempting the daily challenge twice (or two different users attempting it) should have the same results (the same sequence of random numbers). 两次尝试每日挑战(或两个不同的用户尝试)应具有相同的结果(相同的随机数序列)。

I want: 我想要:

  • generate a sequence of random numbers. 生成一个随机数序列。
  • sequence must be the same every time I start generating in the same day 每次我在同一天开始生成时,顺序必须相同
  • get a different sequence of numbers each day 每天得到不同的数字序列

I think I should create a Random with a DateTime as the seed, but I'm not sure how. 我想我应该创建一个以DateTime作为种子的Random ,但是我不确定如何。 DateTime.UtcNow.Ticks is a long and DateTime's seed is an int . DateTime.UtcNow.Ticks是一个long型,而DateTime的种子是一个int

I do not want the hours/minutes/seconds to impact the randomness (aside from the boundary between yesterday and today). 我不希望hours/minutes/seconds影响随机性(除了昨天和今天之间的边界)。 The answers I've found are all about passing the current time into Random (or how that's the default no-parameter ctor behaviour). 我找到的答案都是关于将当前时间传递给Random (或者这是默认的无参数ctor行为)。

// UTC ensures all users see the date flip occur at the same
// time. If you want the date flip to be local for the user's
// time zone, use DateTime.Today instead.
var date = DateTime.UtcNow.Date;
// Generate a seed by combining the year and the day of the year.
// DayOfYear is always gregorian (ignores culture) and always in
// [1,366].
var seed = date.Year * 1000 + date.DayOfYear;
return new Random(seed);

It may be better to generate a sequence of random numbers once each day and store them, so that those numbers can be "replayed" to people who play the challenge during that day. 最好每天生成一次随机数序列并将其存储,以便可以将那些数字“重放”给当天挑战的人们。 This approach may be viable, for instance, if that challenge consists of a randomly generated puzzle game board. 例如,如果该挑战由随机生成的益智游戏板组成,则此方法可能可行。 This approach also has the advantage that the application won't be tied to a particular RNG's implementation. 这种方法还具有以下优势:该应用程序不会与特定RNG的实现绑定。 See also my article on seeded RNGs . 另请参阅我关于种子RNG的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM