Random Library - a better random library

Random Library

Install :arrow_down: Source Code :floppy_disk: how to use :wave:

hello everyone! Today, me and my friend @Biack1st present to our our Random Library module! Using this module you can easily create random chances and get random points in objects!

-- random chance object
local Module = require(game.ReplicatedStorage.RandomLibary)
local myTable = {
	["Cool Item"] = {
		["Item"] = "COOL",
		["Chance"] = 10
	},
	["Not Cool Item"] = {
		["Item"] = "Epic",
		["Chance"] = 200 --- each mini-table inside a loot table MUST have a index called "chance". 
	}
}
print(Module:GetRandomChance(myTable)) -- prints the item the library returned
-- gets a random point inside a part
local Library = require(game.ReplicatedStorage.RandomLibary)
local Part = script.Parent
local RandomPoint = Library:GetRandomPointInPart(Part) -- gets a random point inside the part(cframe)
local NewPart = Instance.new("Part")
NewPart.Anchored = true
NewPart.CFrame = RandomPoint
NewPart.Parent = workspace

-- gets a random point inside a Model
local Library = require(game.ReplicatedStorage.RandomLibary)
local Model = script.Parent
local RandomPoint = Library:GetRandomPointInModel(Model) -- gets a random point inside the model(cframe)
local NewPart = Instance.new("Part")
NewPart.Anchored = true
NewPart.CFrame = RandomPoint
NewPart.Parent = workspace

-- Random point in Region
local Module = require(game.ReplicatedStorage.RandomLibary)
local Region = Region3.new(Vector3.new(100,100,100), Vector3.new(0,0,0))
local RandomPoint = Module:GetRandomPointInRegion(Region.Size, Region.CFrame) -- gets a random point inside the region(cframe)
local Part = Instance.new("Part")
Part.CFrame = RandomPoint
Part.Anchored = true
Part.Parent = workspace

Examples

BUBBLES!!!
This was done using the :GetRandomPointInPart function

Random Chance system!!
You may notice how the “death” option comes more. While the happiness comes less and the epicness comes slightly more than the happiness

Credit

  • HUGE credit to my friend @Biack1st, he wrote the :GetRandomPoint functoins and the github pages!

Outro
I hope this could help you out! If you come across any issues, bugs, have questions, feedback or suggestions then feel free to say them here! byeee

22 Likes

Change your GitHub src file into the src/main.lua file. src is the name of a commonly-used folder, not file.

2 Likes

ok but how do I do so? im a bit new to github

2 Likes

actually my friend did. thx for saying how to do it

1 Like

How does this make random stuff???

curious

1 Like

Basically, it’ll make a number with each object’s chance. This is the sum. It’ll then create a RNG using Random.new. It’ll then get a random number using Rng:NextNumber(0, Sum) . It’ll then loop through all the objects inside the table and see if the Object it’s currently’s chance has less chance than the Random number, it’ll return the object it’s currently on. If it isn’t, it’ll subtract the chance from the random number. Repeat until it gets an object.

@geometricalC2123

You can make GetRandomPointInPart and GetRandomPointInModel with 1 function.

Just use Instance:IsA(“BasePart”) and etc to check if it’s a part or model.

1 Like

it isn’t just random.new:nextnumber. It takes the already existing random data type and transform it into something completely different

Oh yeah. Idk why my friend didn’t think of that when he wrote those functions. Oh well :man_shrugging:

1 Like

I mean, you could add that…

A small criticism: you use typings in your methods but your consistency begins to dip whenever you’re defining variables and auxiliary functions.

Ideally, you should not be using any in your scripts. The point of typing is to prevent runtime errors by alerting you when you provide illegal arguments to a function call. When you use any, whether implicit or explicit, you opt out of it - meaning that if you’re lazy on one day, another day may come where you forget the parameter types of your function, and then you get hit with an exception. And, in the worst case where type coercion is permitted, your argument might be coerced into something you might not expect, leading you to have a bug that sits under your nose for what could be hours, days, maybe weeks.

I may or may not be speaking from experience.

In short, if you care about typing your functions, please go all or nothing.

2 Likes