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:
- Create an account here
- Create a new API key here
- 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
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!