Random2 - Achieve near true random numbers!

Purpose
I’m sure a lot of you are familiar with how mediocre the random libraries provided by Roblox are, because I sure am. When making a game that relies heavily on numbers being truly random, it can be rather game-breaking at times when Roblox’s random number generator returns sub-par results. Random2 will give you the closest thing to truly random results.

Inspiration
Random2 is inspired by, and works in a similar fashion to @AverageLua 's TrueRandom module. It was what I originally used to combat this problem. However, TrueRandom uses random.org’s old API which is significantly slower. But it does not require a key so if you don’t need quick results TrueRandom is an excellent alternative.

How it works
Random2 is rather simple, as it works by sending http requests to random.org. Since http requests are limited Random2 makes each random number request as efficient as possible. Each http request returns a table of multiple random numbers within the requested scope so that no further http requests need to be made when requesting within those bounds. All unused random numbers are cached so that they can be retrieved when another request is made (if they fall between the min/max values).

What happens if the http request fails?
Unfortunately if the http request fails a number generated using Random() will be returned, however a warning will be printed to let you know.

Is this free?
Random2 is free, of course, however you will most likely need to purchase a license on the random.org website. A free license is available, however your requests will be very limited and most likely will not meet the required amount of requests, I have reached the limit of the free license just by testing in studio.


Before you use
An API key is required for this module to work. Here are the steps to do so:

  1. Create an account here
  2. Create a new API key here
  3. Click on the new key you created and copy the API key

Once you have retrieved your key paste the key where it is indicated in the module.


How to use
Random2 is fairly simple to use, it’s usage is similar to that of Random().

Here’s how you create a new random object
Random2.new(min, max, amount [optional])

Once you have created a random object all you need to do is call getNum and a random number will be returned within the scope of the random object’s min/max.

The amount parameter, which is optional, will set how many random numbers should be added to cache for the requested random object. Default is 1000, however I currently don’t see a use in setting a lower amount but the option is there.

Example
For this example let’s say we want to generate a random number between 1 and 10.

local replicated = game:WaitForChild("ReplicatedStorage")
local modules = replicated:WaitForChild("Modules")
local random2 = require(modules.Random2) --Obviously the module location is irrelevant

local randomObj = random2.new(1, 10) --Create the random object
local randomNumber = randomObj:getNum() --Get a random number from that object
print(randomNumber)

Get the module
You can retrieve the module here

I cannot guarantee that I will continue to update this as I am working full time on a big project right now, however I may make changes if I personally notice them or they are big issues that need to be addressed.

Feel free to rework this module to your liking, however credit would always be appreciated :slight_smile:


I hope this is of use to you guys, I spend very little time on this so apologies if it’s unrefined, I just wanted to offer an easy solution to others in the same predicament that I was in. Please do let me know of any bugs/suggestions you have for this module!

Have a great day, and thanks for taking the time to read!

4 Likes

Hm, i didn’t realize that the current randomizers were so bad, I guess this new feature is useful? Uh :neutral_face:

Wouldn’t recommend it

I am working on something that mimics a board game. From the extensive amount of testing I have done, I noticed a LOT of repeated numbers and trends with the random numbers being generated. Once I started using this those repeated numbers and patterns disappeared.

1 Like

Huh, i guess this may be useful.

1 Like

This is exactly why I added safeguards and preventative measures. If it fails it will use Random(). Throttled http requests aren’t much of an issue as repeated requests of the same min/max values won’t trigger an http request.

Can you elaborate?

What do you mean? If the numbers being returned by math.random randomly go 1, 2, 3, and then randomly stop, I’d say that’s still random enough for me because it RANDOMLY happened. I’ll say it’s not random enough when I manage to predict the number without knowing the seed.

For sure, my current project is based off a board game. Which inherently means it relies HEAVILY on numbers being as random as possible. An example of this is mimicking dice rolls. I originally used the new Random() method which worked better than math.random for sure. However I noticed through the extensive game testing I did with myself and members of my team that there would be almost a “trend” of sorts with certain numbers. A number would get rolled WAY more than it should be rolled, which caused the game to be unbalanced. You could argue that it was still random, however I noticed that these “trends” stopped happening after I made this module.

There are some other mechanics that take place in the game where I noticed the same pattern however I would prefer not to disclose them as I would like to keep the nature of my game a secret until it is released.

This might sound crazy, but having numbers not repeat could actually be a sign of things not being random. This reminds me of a particular incident where some developers were getting complaints that their numbers weren’t random enough, so they made it so that numbers don’t repeat (ironically making the numbers arguably less random), and it fixed the issue. I can’t seem to remember who these developers were, but please ping me if someone else does.

You have a good point, however repeating numbers wasn’t exactly the issue, it was but it also wasn’t at the same time. It was more of a pattern that was an issue, more than one number would repeat itself.

With my dice physics an 8 is more likely to be rolled than a 3 for example, as per a real dice. But I would see games have like a 3 and an 8 being rolled almost every second roll for the entire round, and then numbers like 6 and 7 would hardly be rolled if at all. Patterns like this would be prevalent in most round I played when I used Random()