The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## lua randomiztion question??

This is a discussion on lua randomiztion question?? within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; k well i know how to randomize in lua but i want each thing out of the 52(yes i'm makeing ...

 Tweet
1. ## lua randomiztion question??

k well i know how to randomize in lua but i want each thing out of the 52(yes i'm makeing a card game)to be diffrent but i'm haveing trouble currently and this was one way i thought about doing it but it would be a long process and it may also take a while for it to completly randomize but here is what i was thinking:

while true do
if card1==card2 or card1==card3 or card1==card4...so on untill all 52... then card1=math.random
end

but as you can imagine this would be a lengthy process and one i would not wish to do so like i said is their an easyier way to ensure each card is diffrent of is this the only way you can think of

2. Can't lua handle arrays?

3. You should try to put all 52 cards in an array. So in that way you can loop through all of the elements and test for each one instead of having 52 nested if statements.

4. arrays...? sorry but even when i coded in flash i just never got how to do an array i thought an array was like so: cards=[card1=math.random(52), card2=math.random(52),... etc...card52=math.random( 52)]

i tried it but my script just got an error so if i'm not using it correctly or something...

5. Good question. I guess you could store in an array (sorry if my lua syntax is incorrect!):

-- init the deck
deck = {}

for i=1, 52 do
deck[i] = i
end

local shuffled = {}
local i = 0
rndCard = 0

--go thru the whole deck
for i=1, 52 do
--pick a random card
--insert it into our local deck
--remove the card from our passed deck
end

return shuffled
end

I'm sort of new to Lua so when Lua removes elements I'm not sure if it "re-numbers" the array.

6. k thanks i'll try to implement it but i may just fall back on my original plan and thanks again=-)

7. Do yourself a favor and use tables...once you learn how you'll see that you'll need them in almost anything you do, and will make your code much shorter and much easier to deal with

8. Tables=arrays?

9. k i think i'm getting it but why do they put a > symbol? is that just to point out something or does that need to be in the code?

10. yes tables are arrays

And where do you have to put a > symbol? Its just a "bigger than" symbol.

11. k new question: how to you make it so that the array randomizes them to diffrent spots like:
deck={1,2,3,4,5}
deck[math.random(5)]
something like that not completly sure

12. just as a note you can use arrays for a game such as this to do some very useful stuff..

say your deck is 3 cards (lol):
deck = {1,2,3}

Then you could take this and add types to each one...such as the image of the card, and whether it has been dealt or not...etc like so...

deck[1] = { image = Image.load("bleh.png"), dealt = false }
deck[2] = { image = Image.load("bleh.png"), dealt = false }
deck[3] = { image = Image.load("bleh.png"), dealt = false }

You can refer to them in your code like this..
deck[1].image
deck[1].dealt
etc....

13. yes but how would you randomize it so the numbers would show up on the screen in diffrent orders...i just got an idea brb=-)

14. arg can't get it to work see heres what i want it do:

deck={1,2,3...so on so forth..52}
but then i'm not sure what to write so that it then randomizes the array into diffrent spots:

deck={47,32,29...so on so forth...3}

but i'm not sure what a cammand is for randomizing the array to place them in diffrent places

edit: sorry for double post

15. well i made something for the fun of it..not exactly how you just described but i wrote this a bit before that post anyway lol...it gives one result each time you run the program.
technically if it chose more than one it wouldnt allow a card to be chosen if it had already been dealt.

you could kinda use this to make a second array to store the new shuffled deck in with the results of the random choosing..

Code:
```white = Color.new(255,255,255)
math.randomseed(os.time())  -- seed to keep from program repeating random numbers each run

deck = {} -- make deck array
for i = 1,52 do -- make 52 table elemets for cards
deck[i] = { card = i, dealt = false }
end

cardToDeal = nil -- card game will try to deal
dealing = true -- is the game dealing?
dealtCard = 0 -- card that successfully got dealt

function dealCards()
cardToDeal = deck[math.random(52)].card  -- try a random card
if deck[cardToDeal].dealt == false then   -- if that card hasnt been dealt then
dealtCard = deck[cardToDeal].card   -- becomes dealt card
deck[cardToDeal].dealt = true  -- set that card to dealt so it cant be used again
dealing = false  -- no longer dealing
end
end

while true do
screen:clear()

if dealing == true then  -- if dealing then perform the function
dealCards()
end

if dealing == false then  -- if not dealing print the card that got chosen
screen:print(100,100,dealtCard,white)
end

screen.waitVblankStart()
screen:flip()
end```

16. thank you ever so much but i was about to post something that i thought might work
but nevermind, i'm ganna try and work with this and get all 52 cards to display and thank you very much=-)

edit: does this just re randomize the numbers(not store them?)

17. So what exactly is the randomize code? I need it And what code would I use to initate and start a timer? And if I wanted to display that time would I need to assign it to a variable then print it?

18. k well millionair thank you for the code but i unfortuantly have to say that after toying around withit, it's now what i'm exactly looking for, what i'm trying to get is something that well change the order of the array for example:

deck={1,2,3}
after random code
deck={2,3,1}

i hope you understand me more and still thanks

19. Um, did you look at my code?

20. could you explain your code a little better, i prefer to use code that i can understand, i understood millionairs but yours is a tad bit confusing to me

21. look at this code....it takes the array tempdeck[] and makes the cards in order 1-52.
then there is another array made, deck[]. the function fills the deck with random numbers from the first deck, without allowing repeats.
you can press x to see what numbers were assigned.

Code:
```white = Color.new(255,255,255)
math.randomseed(os.time())

tempdeck = {}
for i = 1,52 do
tempdeck[i] = { card = i, dealt = false }
end

deck = { }

dealing = true
cardToDeal = nil
counter = 1
dealtCard = 0
view = 1

function dealCards()
cardToDeal = tempdeck[math.random(52)].card
if tempdeck[cardToDeal].dealt == false then
table.insert(deck, counter, cardToDeal)
tempdeck[cardToDeal].dealt = true
counter = counter + 1
end
if counter == 53 then dealing = false end
end

while true do
screen:clear()

if dealing == true then
dealCards()
end

view = view +1
end
screen:print(100,100,"deck["..view.."] = ".. deck[view],white)

screen.waitVblankStart()
screen:flip()
end```
the if pad():cross sectoin, and oldpad variable, and view variable are not needed. just to show what its doing

22. here it is a step further with a lot of details..this one can also get a new deck by pressing the circle button.

Code:
```white = Color.new(255,255,255)
math.randomseed(os.time())  -- set seed for more random numbers

tempdeck = {} -- we'll pull our numbers for our real deck from this temp deck
for i = 1,52 do  -- assign deck[1] through deck[52] card numbers and deal to false
tempdeck[i] = { card = i, dealt = false }
end

deck = { } -- table for our real deck

getdeck = true   -- if true this will get our random deck
cardToDeal = nil
counter = 1 -- array number to use with table.insert
view = 1 -- only used to view the results

function dealCards()
cardToDeal = tempdeck[math.random(52)].card   -- get a random number from the temp deck
if tempdeck[cardToDeal].dealt == false then    -- if that card hasnt been used yet
table.insert(deck, counter, cardToDeal) -- insert it to the next slot in our decks array
tempdeck[cardToDeal].dealt = true -- set that card to dealt (or used)
counter = counter + 1 -- increase our counter for the next card
end
if counter == 53 then getdeck = false end -- if counter hits 53 then all 52 have been done
end

function resetCards() -- get a new  deck
for i = 1,52 do
deck[i] = nil -- erase our old deck
tempdeck[i].dealt = false -- set all the dealts to false to reuse them again
end
counter = 1 -- set counter back to 1 to restart the deck array
view = 1
end

while true do
screen:clear()

if getdeck == true then -- if we need a new deck do the dealCards function
dealCards()
end

if pad:cross() and oldpad:cross() ~= pad:cross() and view < 52 then -- if x pressed show the next result
view = view +1
end

if pad:circle() and oldpad:circle() ~= pad:circle() and view < 52 then -- if O pressed get a new deck
resetCards()
dealCards()
end

screen:print(100,100,"deck["..view.."] = ".. deck[view],white)

screen.waitVblankStart()
screen:flip()
end```

23. k thanks millionair but i won't be able to try this untill tommorow

24. pspmillionair could you help me a little more i've been using your code and messed around with it so that pics would blit and thusfar this is what i've done:

Code:
```white = Color.new(255,255,255)
math.randomseed(os.time()  )  -- set seed for more random numbers

tempdeck = {} -- we'll pull our numbers for our real deck from this temp deck
tempdeck[1] = { aceofdiamonds=Image.load("aceofdiamonds.png"), dealt = false }
end

deck = { } -- table for our real deck

getdeck = true   -- if true this will get our random deck
cardToDeal = nil
counter = 1 -- array number to use with table.insert
view = 1 -- only used to view the results

function dealCards()
cardToDeal = tempdeck[math.random(52)].card   -- get a random number from the temp deck
if tempdeck[cardToDeal].dealt == false then    -- if that card hasnt been used yet
table.insert(deck, counter, cardToDeal) -- insert it to the next slot in our decks array
tempdeck[cardToDeal].dealt = true -- set that card to dealt (or used)
counter = counter + 1 -- increase our counter for the next card
end
if counter == 53 then getdeck = false end -- if counter hits 53 then all 52 have been done
end

function resetCards() -- get a new  deck
for i = 1,52 do
deck[i] = nil -- erase our old deck
tempdeck[i].dealt = false -- set all the dealts to false to reuse them again
end
counter = 1 -- set counter back to 1 to restart the deck array
view = 1
end

while true do
screen:clear()

if getdeck == true then -- if we need a new deck do the dealCards function
dealCards()
end

if pad:cross() and oldpad:cross() ~= pad:cross() and view < 52 then -- if x pressed show the next result
view = view +1
end

if pad:circle() and oldpad:circle() ~= pad:circle() and view < 52 then -- if O pressed get a new deck
resetCards()
dealCards()
end

screen:blit(100,100,"deck["..view.."] = ".. deck[view])

screen.waitVblankStart()
screen:flip()
end```
but because you did it with a card i'm not sure how to get it to blit a random pic of the cards, so i'll mess around with it a little more but your help is very well helpful and thank you for your time with helping me with this

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 08:49 PM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY