Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you get a truly random number?

local gui = script.Parent

gui:WaitForChild("StartButton").MouseButton1Down:Connect(function()
    local children = g("ReplicatedStorage"):WaitForChild("Questions"):GetChildren()
    if #children > 0 then
        local num = math.random(1, #children) ------
        print(children[num].Value)
        children[num]:Destroy()
    else
        print("end")
    end
end)

I'm using math.random on line 6 to get a random stringvalue, but it almost always returns the first one.. How do I get a truly random one?

2 comments

0

what he will use in base to get the random number?

0

there is no 'true random' but math.randomseed(tick()) should help

2 answers

✓ Accepted answer

Truly random numbers,are often based off of physical phenomenon. Such phenomena could be thermal noise etc. Sadly, this won't work in roblox. Everything here is pseudo-random. Meaning, it is calculated by an initial value, meaning the first number is usually same. Then using algorithms, calculates further numbers. The only way to truely get random numbers in roblox, would have to be based on many things. Maybe a secret code off of players in the server, the current time, player activity etc. Things that will allways vary. Otherwise, you are out of luck. To get more random numbers though, use math.randomseed, to change the seed.

Use the HTTP service to get them off a website which does random numbers.

Random.org is one such site, and it has an API you can use too.

Obviously, sending an HTTP request every time you need some numbers is ridiculous. A much smarter solution would be some sort of stack-based solution. Pseudocode below.

randomNumberStack = []
randomNumberCount = 500

function getNewNumbersFromRandomWebsite(x)
    -- Code here to call Random.org API to fetch x random numbers
    return newRandomNumberArray

function getRandomNumber()
    if (#randomNumberStack == 0) then
        for i, v in pairs(getNewNumbersFromRandomWebsite(randomNumberCount))
            randomNumberStack[#randomNumberStack + 1] = v
    
    return randomNumberStack[#randomNumberStack]

function initialiseRandomNumbers()
    randomNumberStack = getNewNumbersFromRandomWebsite(randomNumberCount)

function main()
    initialiseRandomNumbers()

This discussion is preserved as it was. New answers, comments, and votes are closed.