forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

lua Random Number

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
mafia1ft



Joined: 29 Mar 2006
Posts: 17

PostPosted: Wed Mar 29, 2006 4:48 pm    Post subject: lua Random Number Reply with quote

is ther a way to create a random number in lua
thx mafia1ft
Back to top
View user's profile Send private message
Kameku



Joined: 23 Mar 2006
Posts: 32
Location: Oregon, USA

PostPosted: Wed Mar 29, 2006 4:55 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
mafia1ft



Joined: 29 Mar 2006
Posts: 17

PostPosted: Wed Mar 29, 2006 5:07 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Ats



Joined: 05 Dec 2005
Posts: 37
Location: Biarritz - France

PostPosted: Wed Mar 29, 2006 7:06 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
mafia1ft



Joined: 29 Mar 2006
Posts: 17

PostPosted: Wed Mar 29, 2006 7:23 pm    Post subject: Reply with quote

i still get a error
does the random number give me a number like 2 or 2.546747749
thx
Back to top
View user's profile Send private message
JorDy



Joined: 11 Dec 2005
Posts: 121

PostPosted: Thu Mar 30, 2006 1:07 am    Post subject: Reply with quote

use the math random like this:

Code:

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)

Back to top
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Thu Mar 30, 2006 2:05 am    Post subject: Reply with quote

math.random(1,3) returns only either 1, 2, or 3, nothing between them.
_________________
Geo Massar
Retired Engineer
Back to top
View user's profile Send private message Send e-mail
JorDy



Joined: 11 Dec 2005
Posts: 121

PostPosted: Thu Mar 30, 2006 3:05 am    Post subject: Reply with quote

i didnt realise that i always assumed that since 0,1 returns decimals they all did
Back to top
View user's profile Send private message
Kameku



Joined: 23 Mar 2006
Posts: 32
Location: Oregon, USA

PostPosted: Thu Mar 30, 2006 7:36 am    Post subject: Reply with quote

Lua is case-sensitive, it has to be math.random(1,3) NOT math.Random(1,3)
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
mafia1ft



Joined: 29 Mar 2006
Posts: 17

PostPosted: Thu Mar 30, 2006 2:07 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Kameku



Joined: 23 Mar 2006
Posts: 32
Location: Oregon, USA

PostPosted: Thu Mar 30, 2006 4:51 pm    Post subject: Reply with quote

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

Post what you have right now.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
mafia1ft



Joined: 29 Mar 2006
Posts: 17

PostPosted: Thu Mar 30, 2006 5:46 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
mafia1ft



Joined: 29 Mar 2006
Posts: 17

PostPosted: Thu Mar 30, 2006 6:53 pm    Post subject: Reply with quote

i got it to work and thx to everyone that help or tryed
Back to top
View user's profile Send private message
glynnder



Joined: 04 Sep 2005
Posts: 35

PostPosted: Mon Apr 03, 2006 10:16 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group