How Do I use Random.New()

I am using math.Random for a random game generator.

It works, but apparently math.Random has a limit, and because of that the generator can only get old games. I need something better than Math.Random.

I tried Random.New() but I don’t understand it at all.

1 Like

Random.new() does the same thing as math.random(). math.random just has the problem of only accepting 32 bit integers and not 64 bit integers like the Random library.

-- Usage
local random = Random.new()
local randomInteger = random:NextInteger(1, 15) -- returns a random integer (whole number) between 1 and 15
local randomNumber = random:NextNumber(1, 15) -- returns a random number (decimal) between 1 and 15

print(randomInteger)
print(randomNumber)
5 Likes

Ok i put it in like this:

-------------------Script by Masi14YTB--------------------
local TeleportService = game:GetService("TeleportService")
local gameID = 0

script.Parent.Touched:Connect(function(hit)

	if game.Players:FindFirstChild(hit.Parent.Name) then

		local mamf
		local info

		repeat
			local random = Random.new()
			local randomNumber = random:NextNumber(1, 99999999999999) -- returns a random number (decimal) between 1 and 15
			randomNumber = tonumber(randomNumber)
			
			mamf = randomNumber
		
			info = game:GetService('MarketplaceService'):GetProductInfo(mamf, Enum.InfoType.Asset)
			task.wait()
		until info and info.AssetTypeId==9

		TeleportService:Teleport(mamf, game.Players:FindFirstChild(hit.Parent.Name))

	end

end)

but, i get an error

image

1 Like

The problem is likely occurring because the number that was generated doesn’t point to a real asset.

You can try something like 418141608 to 479141610.

1 Like

i have a system that checks if

which is why i’m confused

if the system finds the number to not be a real asset, it’ll repeat the code until it finds one

1 Like

This has to be wrapped in an error catcher, if it throws an HTTP 400 Error, it will stop the thread:

local success, output = pcall(function()
   return game:GetService('MarketplaceService'):GetProductInfo(mamf, Enum.InfoType.Asset)
end)

if success then -- the call was successful
   info = output -- set the variable to the asset info
end
2 Likes

when i touch the part it does nothing now

1 Like

Small thing to add.

Random.new has a argument you can apply a seed, to alter the randomness of the function, as for math.random, its math.randomseed that would alter the randomness for the said function.
This is usually why you see randomseed(tick()) when trying to get a random number.

Another thing that is unknown to some people is that, you can use math.random without any arguments, and doing this will give you a number between 0 to 1 in decimals, would would be similar to NextNumber if you set the Interval between 0 and 1.

2 Likes