Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do you get a truly random number?

Asked by 6 years ago
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?

0
what he will use in base to get the random number? darkzerobits 92 — 6y
0
there is no 'true random' but math.randomseed(tick()) should help gaberocksall 0 — 6y

2 answers

Log in to vote
1
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago

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.

Ad
Log in to vote
0
Answered by
fredfishy 833 Moderation Voter
6 years ago

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()

Answer this question