WorldRand utility class

Modified on Tue, 28 Jan at 6:27 PM

The WorldRand class provides tools to generate consistent random values linked to specific positions or values in the game world. This system ensures predictable results based on input parameters, making it ideal for procedural generation and other use cases where reproducibility is key.


Methods Overview

1. Randomize

  • Description: Initializes the random number table using a specific seed. This ensures consistent random results across sessions when using the same seed.
  • Usage: Voxel Play uses the world definition seed to initialize it at startup.
WorldRand.Randomize(seed);

2. GetValue

  • Description: Retrieves a random value (0-1) linked to specific inputs.

Variants:

  1. Position-based: Returns a value linked to a Vector3 position.

    float value = WorldRand.GetValue(Vector3 position);
    
  2. Position with Shift: Adds an additional shift parameter to adjust results.

    float value = WorldRand.GetValue(Vector3 position, int shift);
    
  3. Seed-based: Generates a value tied to a seed.

    float value = WorldRand.GetValue(int seed);
    
  4. General Random: Returns a random value from the precomputed table.

    float value = WorldRand.GetValue();
    

3. Range

  • Description: Returns a random value within a specified range.

Variants:

  1. Integer Range:

    • Linked to Position:

      int value = WorldRand.Range(int min, int max, Vector3d position);
      
    • Position with Shift:

      int value = WorldRand.Range(int min, int max, Vector3d position, int shift);
      
    • Without Position:

      int value = WorldRand.Range(int min, int max);
      
  2. Float Range:

    • Linked to Seed:

      float value = WorldRand.Range(float min, float max, int seed);
      
    • Without Seed:

      float value = WorldRand.Range(float min, float max);
      

4. GetVector3

  • Description: Generates a random Vector3 value tied to a position. Useful for procedural content generation.

Variants:

  1. Single Scale:

    Vector3 randomVector = WorldRand.GetVector3(Vector3d position, float scale, float shift = 0);
    
  2. Per-Axis Scale:

    Vector3 randomVector = WorldRand.GetVector3(Vector3d position, Vector3 scale, float shift = 0);
    

Example Usage

  1. Initialize Random Table:

    WorldRand.Randomize(12345);
    
  2. Retrieve a Random Value Linked to a Position:

    Vector3 position = new Vector3(10, 20, 30); float randomValue = WorldRand.GetValue(position);
    
  3. Generate a Random Integer in Range:

    int randomInt = WorldRand.Range(0, 100, new Vector3d(5.5, 10.2, 15.3));
    
  4. Generate a Random Vector3:

    Vector3 randomVector = WorldRand.GetVector3(new Vector3d(2.0, 3.0, 4.0), new Vector3(1.0f, 2.0f, 3.0f));
    

Notes

  • The system ensures predictable randomness for procedural systems.
  • Using the same seed and inputs will always generate the same results, enabling reproducibility.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article