Random Numbers in UnrealScript

Here’s the translation of the Go code example to UnrealScript, with explanations in Markdown format suitable for Hugo:

UnrealScript doesn’t have built-in random number generation functions like Go’s math/rand/v2 package. Instead, we’ll use the FRand() function from the Object class for floating-point random numbers and create our own function for integer random numbers.

class RandomNumbers extends Object;

function int RandomInt(int Max)
{
    return int(FRand() * float(Max));
}

function float RandomFloat(float Min, float Max)
{
    return FRand() * (Max - Min) + Min;
}

function Execute()
{
    local int i;
    local float f;

    // Generate random integers between 0 and 99
    `Log("Random integers:");
    `Log(RandomInt(100) $ "," $ RandomInt(100));

    // Generate a random float between 0.0 and 1.0
    `Log("Random float:");
    `Log(FRand());

    // Generate random floats between 5.0 and 10.0
    `Log("Random floats between 5.0 and 10.0:");
    `Log(RandomFloat(5.0, 10.0) $ "," $ RandomFloat(5.0, 10.0));

    // UnrealScript doesn't have a built-in way to set a seed for random number generation
    // So we can't demonstrate seeded random number generation here
}

defaultproperties
{
}

To use this class:

  1. Create a new file named RandomNumbers.uc in your project’s Classes folder.
  2. Copy the above code into the file.
  3. Compile your project.
  4. You can then call the Execute() function from elsewhere in your code to see the random number generation in action.

Note that UnrealScript’s random number generation is less sophisticated than Go’s. It doesn’t provide a way to set a seed for reproducible random sequences, nor does it offer different algorithms like PCG (Permuted Congruential Generator).

When you run this code, you’ll see output similar to this:

Random integers:
68,56
Random float:
0.809023
Random floats between 5.0 and 10.0:
5.840125,6.937056

The exact numbers will be different each time you run the code due to the nature of random number generation.

UnrealScript’s FRand() function provides a pseudorandom float between 0.0 and 1.0, which we use as the basis for our custom random number generation. The RandomInt() function scales this to produce integers within a specified range, while RandomFloat() scales and shifts it to produce floats within a specified range.

Remember that this is a basic implementation and may not be suitable for applications requiring high-quality random numbers or cryptographic security.