Jump to content

math.random


Recommended Posts

random_number = function(first, second)
    if first < 0 and second < 0 then
        return -math.random(math.abs(first), math.abs(second))
    elseif first > 0 and second < 0 then
        if math.random(2) == 1 then
            return math.random(0, first)
        else
            return -math.random(0, math.abs(second))
        end
    elseif first < 0 and second > 0 then
        if math.random(2) == 1 then
            return -math.random(0, math.abs(first))
        else
            return math.random(0, second)
        end
    else
        return math.random(first, second)
    end
end

 

Solved.

Edited by ZoRRoM
Link to comment
  • Discord Moderators

I cant undestand this junk code, sorry.
Why do you have this much statemenets?

Why not just redefine the values of the variables, and at the end just return it with math.random?
And if you need true randomness:

function totallyRandomRandom(first, second)
    math.randomseed(math.random(0, getTickCount())+getTickCount())
    return math.random(first, second)
end

 

Link to comment
local last
function getRandom(first, second)
	if second > first then
		local rand = math.random(first, second)
		if rand == last then
			return getRandom(first, second)
		end
		last = rand
		return rand
	end
	return false
end

This will get a random number between first and second, making sure its not the same as the last random number you got as well as making sure that its a valid math operation. I hope this helps.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...