lua Random Number

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
mafia1ft
Posts: 17
Joined: Wed Mar 29, 2006 4:45 pm

lua Random Number

Post by mafia1ft »

is ther a way to create a random number in lua
thx mafia1ft
Kameku
Posts: 32
Joined: Thu Mar 23, 2006 12:17 pm
Location: Oregon, USA

Post by Kameku »

You can use the function math.random([[start],end])

Without any specifications, it will return a number between 0 and 1, if you put in one number, it will return a number between 0 and that number, and if you put in two numbers, it will return a number between those.

A good idea would be to put math.randomseed(os.time()) at the beginning of your script, or else it will follow the same "random" sequence.
mafia1ft
Posts: 17
Joined: Wed Mar 29, 2006 4:45 pm

Post by mafia1ft »

can someone please debug this code for me
thx


-- PSPRSP v0.1
-- MADE BY MAFIA1FT

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

sreen:clear()

pad = Controls.read()

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




number = math.Random([[1], 3])



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

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

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


screen.waitVblankStart()
screen.flip()
end
Ats
Posts: 37
Joined: Mon Dec 05, 2005 6:08 am
Location: Biarritz - France
Contact:

Post by Ats »

Et voila!

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

number = math.Random(1,3)

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

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

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

screen.waitVblankStart()
screen.flip()

end
Last edited by Ats on Wed Mar 29, 2006 7:47 pm, edited 1 time in total.
mafia1ft
Posts: 17
Joined: Wed Mar 29, 2006 4:45 pm

Post by mafia1ft »

i still get a error
does the random number give me a number like 2 or 2.546747749
thx
JorDy
Posts: 121
Joined: Sun Dec 11, 2005 8:45 am

Post by JorDy »

use the math random like this:

Code: Select all

math.floor(math.random(1,3))--if the number is a decimal it will take the integer from it (basicly)

math.ceil(math.random(1,3))--if the number is a decimal it will add one to the integer n leave off the decimal part(basicly)

KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

math.random(1,3) returns only either 1, 2, or 3, nothing between them.
Geo Massar
Retired Engineer
JorDy
Posts: 121
Joined: Sun Dec 11, 2005 8:45 am

Post by JorDy »

i didnt realise that i always assumed that since 0,1 returns decimals they all did
Kameku
Posts: 32
Joined: Thu Mar 23, 2006 12:17 pm
Location: Oregon, USA

Post by Kameku »

Lua is case-sensitive, it has to be math.random(1,3) NOT math.Random(1,3)
mafia1ft
Posts: 17
Joined: Wed Mar 29, 2006 4:45 pm

Post by mafia1ft »

has anyone got a math.random to work on there psp because it is still not working but if i tell it number = 1 it works so i know it's math.random that is wrong
Kameku
Posts: 32
Joined: Thu Mar 23, 2006 12:17 pm
Location: Oregon, USA

Post by Kameku »

It's not LuaPlayer, you're using something wrong syntactically.

Post what you have right now.
mafia1ft
Posts: 17
Joined: Wed Mar 29, 2006 4:45 pm

Post by mafia1ft »

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

number = math.random(3)

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

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

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

screen.waitVblankStart()
screen.flip()

end
mafia1ft
Posts: 17
Joined: Wed Mar 29, 2006 4:45 pm

Post by mafia1ft »

i got it to work and thx to everyone that help or tryed
glynnder
Posts: 35
Joined: Sun Sep 04, 2005 9:54 pm

Post by glynnder »

yea, i dislike the case sensitiveness of lua player, i may create something to **drifts away** i have an idea, you'll see it soon
Post Reply