PDA

View Full Version : debug



rowanmcau
March 30th, 2006, 06:43
would someone be able to degug my code
i think something is wrong with my random number thing



DRAW = Image.load("PICS/DRAW.JPG")
WIN = Image.load("PICS/WIN.JPG")
LOSE = Image.load("PICS/LOSE.JPG")

while true do

screen:clear()

pad = Controls.read()

-- X
if pad:cross() then
break
end

a = math.random(3)

if a == 1 then
screen:blit(0,0,WIN)
end

if a == 2 then
screen:blit(0,0,LOSE)
end

if a == 3 then
screen:blit(0,0,DRAW)
end

screen.waitVblankStart()
screen.flip()

end

thx

b8a
March 30th, 2006, 08:50
I don't have any experience with Lua, so sorry if this doesn't help, but the way I've always been told is the standard for generating random numbers in other languages is

math.random()*3
which should give you a floating point number between 0 and 1, multiplied by three, ultimately giving you a floating point number between 0 and 3. So to generate an integer just round the result

math.round(math.random()*3)
The documentation I have doesn't list math.round as being available, but if it isn't you can use either math.ceil or math.floor to descriminate between rounding up or down respectively.

But, looking at the Lua documentation, the way you coded it looks like it should be valid... But, in the past I've had problem with random numbers when I haven't stuck to this methoud, so hope it helps...

yaustar
March 30th, 2006, 12:11
The rand function looks correct.
What numbers are you getting and try seeding the random function.

edit:
Nevermind, the rand function works 100%. The problem is your while loop logic which I leave you to fix as a learning exercise.

I would guess that you have flicking pictures rather then a constant image.