游戏开发中的Random随机函数

Unity C# (CPU)

System.Random

1
2
3
4
5
6
7
8
9
10
11
12
13
var rand = new Random();
rand.Next(); // 非负整数

rand.NextSingle(); // 0-1 float
rand.Sample(); // 0-1 float
rand.Next(Int32 x); // 小于x的非负整数
rand.Next(Int32, Int32);
rand.NextBytes(Byte[]);
rand.NextBytes(Span<Byte>);
rand.NextDouble();
rand.NextInt64();
rand.NextInt64(Int64);
rand.NextInt64(Int64, Int64);

UnityEngine.Random

Range

  • Range(float minInclusive, float maxInclusive)
  • Range(int minInclusive, int maxExclusive)

Seed

1
2
3
int seed = 123;
Random.InitState(seed);
float rand = Random.value;

Color

  • Random.ColorHSV()
  • Random.ColorHSV(float hueMin, float hueMax)

Vector

  • Random.insideUnitCircle
  • Random.insideUnitSphere
  • Random.onUnitSphere

Quaternion

  • Random.rotation
  • Random.rotationUniform

Unity.Mathematics.Random

1
2
3
4
5
6
7
8
9
10
11
12
    NextFloat();
NextFloat(Single);
NextFloat(Single, Single);
NextFloat2();
NextFloat2(float2);
NextFloat2(float2, float2);
NextFloat2Direction();
NextFloat3();
NextFloat3(float3);
NextFloat3(float3, float3);
NextFloat3Direction();
...
1
2
3
4
5
public void Execute(int index)
{
Random rand = Random.CreateFromIndex((uint)index);
Result[index] = rand.NextFloat(0, 100);
}

XOR Shift Random

Unreal

Houdini


游戏开发中的Random随机函数
https://automask.github.io/wild/2021/10/28/lab/S_Random/
作者
Kyle Zhou
发布于
2021年10月28日
许可协议