Very awesome thank you for sharing your knowledge!
No prob - but do test it out that it populates the table with all possible i.e. 1- 80
or just change the highest to 5 or something low rather than doing 80 prints - just to test the loop for piece of mind.
T.
Hi,
i always use this code:
local function getRandomList(count, range) function permute(tab, n, count) n = n or #tab for i = 1, count or n do local j = math.random(i, n) tab[i], tab[j] = tab[j], tab[i] end return tab end do local meta = {} function meta:\_\_index(k) return k end function PositiveIntegers() return setmetatable({}, meta) end end return {unpack( permute(PositiveIntegers(), range, count), 1, count) } end local rnd = {} rnd = getRandomList(20, 80)
it is from http://lua-users.org/wiki/RandomSample and is very fast.
@ToeKnee Thank you, perhaps can you suggest a could way to run a check to see if one of those 20 random numbers matches a predetermined number. For example I would like to see if one of the random numbers matches the number ‘2’. So I tried adding this at the end with no success it will not print if a 2 comes out.
function checknumber()
j=2
if (j==randNum01) then
print(‘yes’)
end
end
Runtime:addEventListener(“enterFrame”, checknumber)
@Toga thanks I ran the code and it didn’t print anything in terminal I tried ’ print( rnd )’ am missing something?
Hi,
in this example:
local rnd = {}
rnd = getRandomList(20, 80)
you get an array back (rnd[1] to rnd[20]) with 20 random number (in a range from 1 to 80) without repeating any. For example if you write print( rnd[1] ) it will print the first number in the array.
Thank you Toga, now I have 2 weapons in my arsenal! If you had to print all 20 numbers(terminal) and run a check to see if one of those random numbers matches the number ‘2’ would you do it like this?
[lua]
print( rnd[1] )
print( rnd[2] )
print( rnd[3] )
print( rnd[4] )
print( rnd[5] )
print( rnd[6] )
print( rnd[7] )
print( rnd[8] )
print( rnd[9] )
print( rnd[10] )
print( rnd[11] )
print( rnd[12] )
print( rnd[13] )
print( rnd[14] )
print( rnd[15] )
print( rnd[16] )
print( rnd[17] )
print( rnd[18] )
print( rnd[19] )
print( rnd[20] )
function checknumber()
j=2
if (j==rnd[1])or(j==rnd[2])or(j==rnd[3])or(j==rnd[4])or(j==rnd[5])or(j==rnd[6])or(j==rnd[7])or(j==rnd[8])or(j==rnd[9])or(j==rnd[10])or(j==rnd[11])or(j==rnd[12])or(j==rnd[13])or(j==rnd[14])or(j==rnd[15])or(j==rnd[16])or(j==rnd[17])or(j==rnd[18])or(j==rnd[19])or(j==rnd[20])then
print(‘yes’)
end
end
checknumber()
[/lua]
Hi, i would do it in a for loop because i would be to lazy to write all of the or clauses. And of course you are much more flexible with the for loop.
for i=1, #rnd do
if rnd[i] == 2 then
print(“yes”)
break
end
end
Hi,
The way to do it would be within a loop - and actually possible within original loop i think
by adding a test and a flag
if randNum01 == “2” then
flag2 = true
end
[lua]
LowhestN = 1
HighestN = 80
owMany = (HighestN - LowhestN) + 1
cntUp = 0
ran = {}
for i = 1, owMany do
ran[i] = LowhestN + cntUp
cntUp = i
print( i, “ran”, ran[1] )
end
for i = 1, 20 do
q01 = math.random(#ran)
randNum01 = ran[q01] – chose a number from the table (Position).
table.remove(ran, q01) --remove that number.
if randNum01 == “2” then
flag2 = true
end
end
[/lua]
I’m half asleep but this code works for printing out to the terminal - if you wanted to keep the 20 numbers selected then you would obviously need to store them & the best way for that would be another table.
my20Num = {}
and then adding
my20num[i] = randNum01
within the loop
also if you wanted to know which of the randoms = “2”
then create a numIs2 = {}
and add it into the loop with an if else statement.
I’m only a new programmer myself - and tend to go towards the simple and obvious rather than the complex and stylish code.
With your Keno i think you might want to store the 20 numbers - so you can use text objects to display, and since the numbers are random would need to know the location within the table of 20 that matchup to your players numbers hence the table of numIs2.
loops as always are your friend in this case.
Again just going by code logic here -( have not tried the additions )
T.
oh in my example the 2 is “2” as a text so try it with 2 as number as well
Ha that is much better thank you very much!
Your welcome.
I’m just developing my use of tables and cleaning up my codes - with your keno project, which could require a lot of number crunching the combination of tables WITH loops can drastically reduce your lines of code and make it look quite elegant.
And this is coming from someone who has in the past done comparisons on multiples the hard way resulting in blocks of 300 -400 lines of code for something that can be done in less than 30 lines with tables and loops.
T.
Yes with my KENO project my next step is to create 80 numbers you can click. I think I will do 10 widgets per row with 8 rows. Instead of placing each one individually with x,y placements I think this will also be an opportunity to use a for loop would you agree?
I didn’t see this mentioned, but the technique is called “shuffling”, like you shuffle a deck of cards. You then pull one card at a time until you run out and shuffle the deck again. I think the functions provided to the shuffling. I forget who I got this from, but this is the one I keep tucked in my library of useful things:
local function shuffleTable(t) local rand = math.random assert(t, "table.shuffle() expected a table, got nil") local iterations = #t local j for i = iterations, 2, -1 do j = rand(i) t[i], t[j] = t[j], t[i] end end
@russm305 - yes using a loop for the X & Y positions is possible when your widget variable names belong to a table.
@Rob - yes i think “shuffling” or “shuffle” might have been in my google search.
Would it be possible to explain your code line by line?
assert - ?
local iterations = #t - #t gives you the number of data in table t - correct?
then the reverse loop - which only goes to 2 (not the 1st??)
t[i], t[j] = t[j], t[i] ???
Thanks
T.
assert generates an error if the conditions are not met. In this case if you don’t pass in a table it will generate an error with the message in the string reminding you to pass it a table.
#t returns the length of the table. This really only works if there are no nil entries in the table.
The next loop starts with the full table and swaps a random entry into the last slot. It then decrements the number of entries. This continues to swap items until the list is down to the last item, which doesn’t need swapped.
Thanks Rob - I was still stumped on one line t[i],t[j] = t[j], t[i] —> (or to show it better T[a],T[b] = T[b], T[a] )
But it finally clicked somewhat - I was stuck on looking at the T[b] = T[b] rather than looking at { T[a],T[b] } = { T[b], T[a] }
this shows the logic of swapping position But still there’s a bit of magic in that line that eludes my understanding lets just call it the mystery of the comma “,”
T.
In Lua, functions (or things on the right hand side of the equal sign can return multiple values:
local function X() return 1, 2 end local a, b = X()
Will cause variable a to have the value of 1 and variable b to have the value of 2. So in this case: t[i], t[j] = 1, 2 would put 1 into t[i] and 2 into t[j]. Hopefully that will help you understand how the swap works.
Thanks Rob,
My head computer finally clicked the concept.
I believe the trick for understanding that line is rather than seeing -
VAR , VAR = VAR, VAR
but rather as
VAR, VAR = value, value So focus on the Variable’s VALUE not the VAR itself
Thanks again for the little lesson.
T.