| View previous topic :: View next topic |
| Author |
Message |
mafia1ft
Joined: 29 Mar 2006 Posts: 17
|
Posted: Wed Mar 29, 2006 4:48 pm Post subject: lua Random Number |
|
|
is ther a way to create a random number in lua
thx mafia1ft |
|
| Back to top |
|
 |
Kameku
Joined: 23 Mar 2006 Posts: 32 Location: Oregon, USA
|
Posted: Wed Mar 29, 2006 4:55 pm Post subject: |
|
|
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 |
|
 |
mafia1ft
Joined: 29 Mar 2006 Posts: 17
|
Posted: Wed Mar 29, 2006 5:07 pm Post subject: |
|
|
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 |
|
 |
Ats
Joined: 05 Dec 2005 Posts: 37 Location: Biarritz - France
|
Posted: Wed Mar 29, 2006 7:06 pm Post subject: |
|
|
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 |
|
 |
mafia1ft
Joined: 29 Mar 2006 Posts: 17
|
Posted: Wed Mar 29, 2006 7:23 pm Post subject: |
|
|
i still get a error
does the random number give me a number like 2 or 2.546747749
thx |
|
| Back to top |
|
 |
JorDy
Joined: 11 Dec 2005 Posts: 121
|
Posted: Thu Mar 30, 2006 1:07 am Post subject: |
|
|
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 |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Thu Mar 30, 2006 2:05 am Post subject: |
|
|
math.random(1,3) returns only either 1, 2, or 3, nothing between them. _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
JorDy
Joined: 11 Dec 2005 Posts: 121
|
Posted: Thu Mar 30, 2006 3:05 am Post subject: |
|
|
| i didnt realise that i always assumed that since 0,1 returns decimals they all did |
|
| Back to top |
|
 |
Kameku
Joined: 23 Mar 2006 Posts: 32 Location: Oregon, USA
|
Posted: Thu Mar 30, 2006 7:36 am Post subject: |
|
|
| Lua is case-sensitive, it has to be math.random(1,3) NOT math.Random(1,3) |
|
| Back to top |
|
 |
mafia1ft
Joined: 29 Mar 2006 Posts: 17
|
Posted: Thu Mar 30, 2006 2:07 pm Post subject: |
|
|
| 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 |
|
 |
Kameku
Joined: 23 Mar 2006 Posts: 32 Location: Oregon, USA
|
Posted: Thu Mar 30, 2006 4:51 pm Post subject: |
|
|
It's not LuaPlayer, you're using something wrong syntactically.
Post what you have right now. |
|
| Back to top |
|
 |
mafia1ft
Joined: 29 Mar 2006 Posts: 17
|
Posted: Thu Mar 30, 2006 5:46 pm Post subject: |
|
|
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 |
|
 |
mafia1ft
Joined: 29 Mar 2006 Posts: 17
|
Posted: Thu Mar 30, 2006 6:53 pm Post subject: |
|
|
| i got it to work and thx to everyone that help or tryed |
|
| Back to top |
|
 |
glynnder
Joined: 04 Sep 2005 Posts: 35
|
Posted: Mon Apr 03, 2006 10:16 pm Post subject: |
|
|
| 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 |
|
 |
|