Quick Lua Issue

here is my issue:


function randomizer(value)
    local n = math.random(0,1)
        if n == 1 then
return true
    elseif n == 0 then
return false
end
end

function engineY()
local car = getPedOccupiedVehicle(jij)
setVehicleEngineState(car, true)
setTimer(setVehicleEngineState, math.random(1000, 2000), 1, car, false)
setTimer(setVehicleEngineState, math.random(5000, 8000), 1, car, true)
setTimer(setVehicleEngineState, 9000, 1, car, false)
setTimer(setVehicleEngineState, 9200, 1, car, true)
setTimer(setVehicleEngineState, 9400, 1, car, false)
setTimer(setVehicleEngineState, 9600, 1, car, false)
--setTimer(setVehicleEngineState, 1, car, math.random(0, 1))
setVehicleEngineState(car, randomizer())
	end

I am trying to get the engine state to randomly select True or False… The last line doesn’t seem to do anything so please tell me what I am doing wrong?

Thanks in advance

This should do the trick:
[lua]
setVehicleEngineState(car, math.random(0,1) == 1)
[/lua]

Doesn’t seem to work… False’s Everytime.

Any other suggestions?
Here is what it looks like now:


function randomizer(value)
    local n = math.random(0,1)
        if n == 1 then
return true
    elseif n == 0 then
return false
end
end

function engineY()
local car = getPedOccupiedVehicle(jij)
setVehicleEngineState(car, true)
setTimer(setVehicleEngineState, math.random(1000, 2000), 1, car, false)
setTimer(setVehicleEngineState, math.random(5000, 8000), 1, car, true)
setTimer(setVehicleEngineState, 9000, 1, car, false)
setTimer(setVehicleEngineState, 9200, 1, car, true)
setTimer(setVehicleEngineState, 9400, 1, car, false)
setTimer(setVehicleEngineState, 9600, 1, car, false)
setVehicleEngineState(car, math.random(0,1) == 1)
	end

[editline]11th October 2013[/editline]

It seems to go right over it and not even notice. Because I changed the last settimer to true

and the car starts everytime now, which i do not want

Even if it’s false every time, it doesn’t mean that it doesn’t work, it just means that the random function keeps returning 0 instead of 1.

Yes thats what I thought to. But then I changed


setTimer(setVehicleEngineState, 9600, 1, car, false)
setVehicleEngineState(car, math.random(0,1) == 1)

To


setTimer(setVehicleEngineState, 9600, 1, car, true)
setVehicleEngineState(car, math.random(0,1) == 1)

And I got true every single time…

math.random returns a float so there’s an absolutely tiny chance that it’ll ever equal 1. You want to check if it’s greater or equal to 0.5 instead.

math.Random returns a float, math.random returns an integer.

There is no such function as math.Random, regardless your statement is true if you give math.random an argument. He could just call it as:
[lua]math.random()[/lua] for it to work.

he’s using it right.

As of now since I don’t know what your functions arguements, try this:
[lua]
function engineY()
local car = getPedOccupiedVehicle(jij)
setVehicleEngineState(car, true)
setTimer(setVehicleEngineState, math.random(1000, 2000), 1, car, false)
setTimer(setVehicleEngineState, math.random(5000, 8000), 1, car, true)
setTimer(setVehicleEngineState, 9000, 1, car, false)
setTimer(setVehicleEngineState, 9200, 1, car, true)
setTimer(setVehicleEngineState, 9400, 1, car, false)
setVehicleEngineState(car, math.random(0,1) == 1)
setTimer(setVehicleEngineState, 9600, 1, car, true)
end
[/lua]



math.random() > 0.5


You should do as thelastpenguin says - that’s the simplest way.

math.Rand is the float function, math.random returns integers.

I’m fairly sure math.random() will give a float if you don’t give it any arguments.

Oh sorry I was thinking of math.Rand

If you don’t give it any arguments, math.random returns a float between 0 and 1, but with arguments it only gives you integers.

This is the code for math.Rand:


return low + ( high - low ) * math.random()

It just is a helper function that returns a float between min and max.