[英]How to have two states of randomness in unity3d, c#?
I am trying to achieve something like two states of random number generates 我正在尝试实现随机数生成两种状态
Example , one instance with seed and other with default randomness: 示例,一个实例具有种子,另一个实例具有默认随机性:
UnityEngine.Random.InitState(255);
I need this because i have a part of my_game as multiplayer with game_a and my_game has some additional features which also uses randomness. 我需要这样做,因为我作为my_game的一部分与game_a一起玩,而my_game具有一些使用随机性的附加功能。
How can I achieve this? 我该如何实现?
Use an instance of the Random class as documented here: https://msdn.microsoft.com/en-us/library/ctssatww(v=vs.110).aspx 使用此处记录的Random类的实例: https : //msdn.microsoft.com/zh-cn/library/ctssatww(v=vs.110).aspx
using System;
using System.Threading;
public class Example
{
public static void Main()
{
Random rand1 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
Random rand2 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
Thread.Sleep(20);
Random rand3 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
ShowRandomNumbers(rand1);
ShowRandomNumbers(rand2);
ShowRandomNumbers(rand3);
}
private static void ShowRandomNumbers(Random rand)
{
Console.WriteLine();
byte[] values = new byte[4];
rand.NextBytes(values);
foreach (var value in values)
Console.Write("{0, 5}", value);
Console.WriteLine();
}
}
You can use two instances of the Random class. 您可以使用Random类的两个实例。
Define that you want to use the System.Random instead of UnityEngine.Random: 定义您要使用System.Random而不是UnityEngine.Random:
using RNG = System.Random;
and then 接着
RNG rand1 = new RNG();
RNG rand2 = new RNG(5);
The first one will use a seed that is depending on the time your game is started, so it will always be different. 第一个将使用种子,该种子取决于游戏开始的时间,因此它总是不同的。 The second one has a set seed and should always produce the same sequence of random numbers 第二个有一个固定的种子,应该总是产生相同的随机数序列
EDIT to show the usage of C# Random over UnityEngine Random 编辑以显示C#Random在UnityEngine Random上的用法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.