Need help with some Logic

Hey guys,

I’m trying to create a game that involves some kind of lottery, where and random generator would pick a random ball, then I could assign that ball. I can do all of this fairly easily, my dilemma is that how would I then pick another ball without getting the same ball again?
Thanks

Ollie

P.S all of the ball are numbered so I can’t deleted it from the movie clip. Or can i assign numbers to each picture in a frame? [import]uid: 67534 topic_id: 28868 reply_id: 328868[/import]

I have not tried this but it seems it may help with assigning numbers/names to each frame; http://developer.coronalabs.com/reference/index/objectsetlabels [import]uid: 52491 topic_id: 28868 reply_id: 116305[/import]

Ah ok, I did take a quick look at that before but I will concentrate on it more now.

Any ideas on how to make a number generator so that say its 1 to 10 and 4 gets called. How could i make it so it doesn’t call 4 again? Would i have to make it check to see if that number has been called and then find another one again if it has been called?

Thank you very much Peach for the quick answer by the way :smiley:
Ollie [import]uid: 67534 topic_id: 28868 reply_id: 116307[/import]

Hey @ollie.brown. This is a very very simple task, it’s called shuffling. Just like you would shuffle a deck of cards.

local function shuffle(t)  
 local iterations = #t  
 local j  
  
 for i = iterations, 2, -1 do  
 j = random(i)  
 t[i], t[j] = t[j], t[i]  
 end  
end  
  
local numbers = {}  
for i = 1, 56 do  
 numbers[i] = i  
done  
shuffle(numbers)  

Now you have an array of numbers that are randomized. If you want to show 6 then:

local balls = {}  
  
for i = 1, 6 do  
 balls[i] = display.newImageRect(string.format("images/balls%02d.png",i), 64,64)  
 -- rest of the code to display the balls  
end  

That last bit makes a few assumptions, first your ball graphics are in a folder called “images” and that each ball is named ball01.png, ball02.png, etc. and that they are 64x64 in size. You of course would substitute whatever you oneed for that. The point was to show you that you would just pick off the first X number of balls after you shuffled the array.
[import]uid: 19626 topic_id: 28868 reply_id: 116329[/import]

Hey robmiracle,

Wow what can I say? Thank you very much indeed for the detailed response!

So pretty much, I need to replace the numbers bit of the code for the lower ball bit. Do you think this would work ok, if for every ball drawn, that ball got taken away from the movie animation?
Thank you very much again!
Ollie [import]uid: 67534 topic_id: 28868 reply_id: 116332[/import]

@ollie.brown I can’t really answer the last question too well not knowing what it is your trying to do, but it sounds reasonable. [import]uid: 19626 topic_id: 28868 reply_id: 116334[/import]