JavaRandom v1.0.1 | A pure Lua implementation of Java's Random class

I was reading this post of someone recreating Minecraft’s classic terrain generation which requires a Lua implementation of Java’s Random class for 1:1 results, since the original poster didn’t share his, I decided to create my own and share it with all of you.

Overview

The JavaRandom Lua module emulates Java’s Random class behavior for generating pseudo-random numbers. It includes functionalities for generating random numbers of various types, seeding the generator, and obtaining random values within specified ranges. This module uses BigInteger for 64 bit integers, credit to Blockzez. This is my first topic so expect editorial edits and future updates.

Where do I get it?

You can get it from here:
1.0.1:

1.0.0:

Documentation

The module consists of methods and constants replicating Java’s Random class functionalities:

  • JavaRandom.new(seed: number | string): Creates a new JavaRandom object with an optional seed value.
  • JavaRandom:next_signed(bits: number) -> number: Generates a random signed integer with the specified number of bits.
  • JavaRandom:next(bits: number) -> number: Generates a random integer with the specified number of bits.
  • JavaRandom:setSeed(seed: string | number): Sets the seed for the random number generator. If you want to use big integers (64 bit) seeds then use strings (“12345678910111213141516” for example).
  • JavaRandom:nextInt(bound: number) -> number: Generates a random integer within a specified range.
  • JavaRandom:nextBoolean() -> boolean: Generates a random boolean value.
  • JavaRandom:nextFloat() -> number: Generates a random floating-point number between 0.0 and 1.0.
  • JavaRandom:nextGaussian() -> number: Generates a random Gaussian (normal) distributed number.
  • JavaRandom:nextDouble() -> number: Generates a random double-precision floating-point number between 0.0 and 1.0.
  • JavaRandom:nextLong() -> BigInteger: Generates a random 64-bit integer.

Usage

To use the JavaRandom module, create an instance and call the provided methods to obtain random values or perform operations.

Example:

local JavaRandom = require(game.ReplicatedStorage.JavaRandom)

-- Create a new instance with an optional seed
local rand = JavaRandom.new(54321)

-- Set a new seed
rand:setSeed(123456)

for i = 1, 10 do
    print(rand:nextInt(100)) -- Adjust the argument to change the range of random numbers
end
19 Likes

You’re aware that Random already exists? What does this implementation provide that the builtin class doesn’t?

1 Like

This is helpful for those who want an exact 1:1 algorithm as Java’s. It has very niche use cases, but it can be helpful when needed!

11 Likes

I forgot that the input seed can also be a “long” primitive in Java, so I implemented that as well, I will update this resource with it soon. (And 64 bit integers must be input as strings obviously)

Alright just updated with support for 64 bit long seeds using BigInteger, it must be input as a string though, and getting a random integer is much slower than when using a 53 bit lua number seed since the long seeds are emulated with BigInteger. So only use string seeds if you really need to.

Using nextint just errors out with the message “attempt to perform arithmetic (mul) on nil and number”
I’m using parallel lua

nvm I was just doing it wrong I forgot to use the right variable

Can i use this in a game i am making?

1 Like

Whats the point of :band(BigInteger.new('4294967295')) in JavaRandom:nextLong()?

.nextLong() gives a lot of incorrect values with :band(BigInteger.new('4294967295'))


(left Java, right Luau)
But without it gives accurate results

1 Like

To be honest because Longs have to be emulated since Lua numbers are 53 bits and it is so much slower, the implementation was really lazy and as long as I saw the output was the same I called it a day. But thanks for noticing that!