Skip to content

DCFApixels/Quasi_Random

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quasi Random Generator

Readme Languages:

Русский

English

中文

Table of Contents


Introduction

Quasi-random sequences are well suited when the problem is to uniformly fill a space by simulating random filling. The implementation is based on a new additive recursive R-sequence and an article from Habr. R-sequence is easy to compute, and when computed in integers it gives good performance.

I used this sequence to generate starting positions, stores and other activity points in the БольКрафт map generator for Warcraft 3. This way the activity points were placed roughly evenly across the map while making their positions look random.


Installation

To install, simply copy the contents of the src folder into the project. The src folder contains:

  • src\QuasiRandom.cs - basic, no environment dependencies;
  • src\QuasiRandom.unity.cs - extension of the basic one to support vectors from Unity;
  • src\QuasiRandom.unity.mathematics.cs - extension of basic to support vector types from Unity.Mathematics;

Description

QuasiRandom is implemented as a 4 byte structure, can generate sequences with uniform filling for 1D, 2D, 3D and 4D space.

Constructors

  • new QuasiRandom() - instance with seed = 0;
  • new QuasiRandom(seed) - instance with seed = seed;
  • QuasiRandom.AutoSeed() - instance with auto seed;

Generation

QuasiRandom.cs
  • bool
    [false - true]
    NextBool() NextBool2(out x, out y) NextBool3(out x, out y, out z) NextBool4(out x, out y, out z, out w);

  • int
    [int.MinValue <= x <= int.MaxValue]
    NextInt() NextInt2(out x, out y) NextInt3(out x, out y, out z) NextInt4(out x, out y, out z, out w);

  • int
    [0 <= x < max]
    NextInt(max) NextInt2(max, out x, out y) NextInt3(max, out x, out y, out z) NextInt4(max, out x, out y, out z, out w);

  • int
    [min <= x < max]
    NextInt(min, max) NextInt2(min, max, out x, out y) NextInt3(min, max, out x, out y, out z) NextInt4(min, max, out x, out y, out z, out w);

  • uint
    [uint.MinValue <= x <= uint.MaxValue]
    NextUInt() NextUInt2(out x, out y) NextUInt3(out x, out y, out z) NextInt4(out x, out y, out z, out w);

  • uint
    [0 <= x < max]
    NextUInt(max) NextUInt2(max, out x, out y) NextUInt3(max, out x, out y, out z) NextUInt4(max, out x, out y, out z, out w);

  • uint
    [min <= x < max]
    NextUInt(min, max) NextUInt2(min, max, out x, out y) NextUInt3(min, max, out x, out y, out z) NextUInt4(min, max, out x, out y, out z, out w);

  • long
    [long.MinValue <= x <= long.MaxValue]
    NextLong() NextLong2(out x, out y) NextLong3(out x, out y, out z) NextLong4(out x, out y, out z, out w);

  • ulong
    [ulong.MinValue <= x <= ulong.MaxValue]
    NextULong() NextULong2(out x, out y) NextULong3(out x, out y, out z) NextULong4(out x, out y, out z, out w);

  • float
    [0.0f <= x < 1.0f]
    NextFloat() NextFloat2(out x, out y) NextFloat3(out x, out y, out z) NextFloat4(out x, out y, out z, out w);

  • double
    [0.0d <= x < 1.0d]
    NextDouble() NextDouble2(out x, out y) NextDouble3(out x, out y, out z) NextDouble4(out x, out y, out z, out w);

  • VectorX
    [0.0f <= x < 1.0f]
    NextVector2() NextVector3() NextVector4();

QuasiRandom.unity.cs
  • VectorX
    [0.0f <= x < 1.0f]
    NextUnityVector2() NextUnityVector3() NextUnityVector4();

  • VectorXInt
    [0.0f <= x < 1.0f]
    NextUnityVector2Int() NextUnityVector3Int();

QuasiRandom.unity.mathematics.cs
  • bool
    [false - true]
    NextBool2() NextBool3() NextBool4();

  • int
    [int.MinValue <= x <= int.MaxValue]
    NextInt2() NextInt3() NextInt4();

  • int
    [0 <= x < max]
    NextInt2(max) NextInt3(max) NextInt4(max);

  • int
    [min <= x < max]
    NextInt2(min, max) NextInt3(min, max) NextInt4(min, max);

  • uint
    [uint.MinValue <= x <= uint.MaxValue]
    NextUInt2() NextUInt3() NextInt4();

  • uint
    [0 <= x < max]
    NextUInt2(max) NextUInt3(max) NextUInt4(max);

  • uint
    [min <= x < max]
    NextUInt2(min, max) NextUInt3(min, max) NextUInt4(min, max);

  • float
    [0.0f <= x < 1.0f]
    NextFloat2() NextFloat3() NextFloat4();

  • double
    [0.0d <= x < 1.0d]
    NextDouble2() NextDouble3() NextDouble4();

State

  • GetState() - Receive state;
  • SetState(state) - Change state;

Other

The methods Equals, GethashCode, ToString, and comparison operators have been overridden.


Example/Comparison

Compares System.Random and QuasiRandom using the example of generating points in two-dimensional space: ex