[SOLVED] How to get random numbers?

Hi Corona Devs ,

I have a question that I did not find any answer and so I would like to ask you for some help if possible.

So, what I need is just the following:

How would I get to generate 6 random numbers that can`t be showed repeated (each one must be unique) from a list starting 00~60?

Easy way to go? Some idea? If you do, please, share with me. I would be very happy of it!

Thank you in advance ,

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20006 reply_id: 320006[/import]

try this
[lua]local numbers = {}
–populating table with 5 numbers (1-5)
for i =1,5 do
numbers[i] = i
end

–loop to generate 5 random numbers
for i = 1, 5 do
– select random position from the table
local pos = math.random(1,#numbers)
local num = numbers[pos]
print(num)
–remove the number from table once it is selected
table.remove (numbers,table.indexOf(numbers, num))
end [/lua] [import]uid: 71210 topic_id: 20006 reply_id: 78026[/import]

Hi Technowand ,

First of: THANK YOU! You`re a genius mate. Ha, ha, ha, 1 shoot - 1 goal! :slight_smile:

It worked nicely. I have just tweaked it a little for my own needs and it`s doing (printing) very well!

Ill start to work with it trying to implement and code my "full" idea into something. :) Ive appreciated it, really.

PS: Ive noted right now that Ive a copy of your Corona “book” that I bought from you when I`ve started here few months ago. Nice pdf btw…just needing some kind of “UP”. :slight_smile:

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20006 reply_id: 78036[/import]

good to know that it worked… looking forward to see the full idea implemented…
thanks for purchasing the book… yes I know that its past due an update… sorry for that… was very busy for the last few weeks. will be sitting soon on that… :slight_smile: for the time if you have any doubts feel free to ask me…
[import]uid: 71210 topic_id: 20006 reply_id: 78055[/import]

Hey Technowand, no worry. When you get some free time and feel to update something there, that`s good. :slight_smile:

BTW, thanks one more time and please, feel free to follow me on Twitter if you want - http://twitter.com/RSCdev - as like this the contact can be even more quick. :slight_smile:
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20006 reply_id: 78120[/import]

@technowand, thanks for function, its awesome [import]uid: 16142 topic_id: 20006 reply_id: 78350[/import]

This is a great code example. I need something like this as well.

The only question is where do you specify the numerical range in that code example? For example, if I wanted 5 unique numbers between 10-80, where would that range go in the code?

Thanks so much! [import]uid: 17827 topic_id: 20006 reply_id: 78854[/import]

@thedog

[lua]local numbers = {}
–populating table with 70 numbers (10-80)
for i =10,80 do
numbers[i] = i
end

–loop to generate 5 random numbers
for i = 1, 5 do
– select random position from the table
local pos = math.random(1,#numbers)
local num = numbers[pos]
print(num)
–remove the number from table once it is selected
table.remove (numbers,table.indexOf(numbers, num))
end [/lua]

I think it should work as you have asked above. Just try and report back if possible. :]
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20006 reply_id: 78857[/import]

thanks @RSCdev

but in the above code you may get nil values sometimes.
so you can try this instead
[lua]local numbers = {}
–populating table with 70 numbers (10-80)
for i =10,80 do
numbers[#numbers+1] = i
end

–loop to generate 5 random numbers
for i = 1, 5 do
– select random position from the table
local pos = math.random(1,#numbers)
local num = numbers[pos]
print(num)
–remove the number from table once it is selected
table.remove (numbers,table.indexOf(numbers, num))
end [/lua] [import]uid: 71210 topic_id: 20006 reply_id: 78861[/import]

Thanks to you both, @RSCdev and renvis@technowand.

And thanks for the code example. I am new to Lua programming and still trying to learn and greatly appreciate the quick response.

I am getting to that portion of the coding soon, and will definitely post how it goes.

Again, thanks so much. [import]uid: 17827 topic_id: 20006 reply_id: 78862[/import]

Hey guys,

It`s me to thanks @Technowand. :slight_smile:

You`re very welcome as well @thegdog!
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20006 reply_id: 78867[/import]

@Technowand

THANK YOU! Genius!!

I am already using it to not show the same objects multiple time on the screen. Works like a charm. I am going to try to use it this time to not have the object overlap on the screen. The objects have radius R and I want to put up to 3 objects on the screen at the same time (maybe more later)

Should be a fun thing to try…

Thanks a lot. Appreciate you sharing this great piece of code.

Mo [import]uid: 49236 topic_id: 20006 reply_id: 79111[/import]

Hey guys, am back with one question more for @Technowand.

So, based on your code, I would like to know HOW would I get this exactly same code working for showing display objects (images) instead “printing” the result of the variable “num”? :\

What Id like to do is like "populate" the table with X number of images (already declared with their X and Y positions pre-defined/set) of cartoons instead only printing the "numbers" and using your code to show these random selected images with some delay as using timer or transition I dont know (as it normally do showing the numbers randomly selected from the initial table).

Do you get it? Does it make sense? I hope so. :slight_smile:
By the way, so much thanks for the given help, I appreciate it.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20006 reply_id: 79453[/import]

hi @RSCdev
hope this is what you are after.

[lua]–populating table with Images
local images = {}
images[1] = {“img1.png”,100,200}
images[2] = {“img2.png”,150,250}
images[3] = {“img3.png”,80,90}
images[4] = {“img4.png”,250,320}
images[5] = {“img5.png”,320,150}

local function showImage()
– select random position from the table
local pos = math.random(1,#images)
local image = display.newImage(images[pos][1],images[pos][2],images[pos][3])
–remove the image from table once it is selected
table.remove (images,pos)
end
timer.performWithDelay(1500,showImage,5)[/lua] [import]uid: 71210 topic_id: 20006 reply_id: 79579[/import]

Hey @renvis@technowand - THANK YOU! I really apreciate your help so much!:slight_smile:
My best regards mate,
Rodrigo. [import]uid: 89165 topic_id: 20006 reply_id: 79669[/import]

thanks for the code. this is an interesting trick to delete the selected item from the table…

i did not understand how it really work ?  so when you delete the item… why the random function does not select the same index again…

if we assume the table has the following : 1,2,3,4,5… #numbers is 5

if num= 4 … then 4 will be removed from table… #numbers is 4

next time the random will select number between(1-4) … will the last number in the table (5) be shifted automatically to position 4…

this what i could not get…  because if that is not the case then we should get nil some times.

Regards

Abdul

Do not forget about seed of rundom generator. If you do not specify new, then you will always use the same one and generaly each time you will get random but the same combination.

math.randomseed( os.time() ) ----> a different sequence each time if there is a long enough time between invocations

yeah but why we dont get nil value in this case… just to understand the algorithm if you have time :) 

table.delete() removes the entry and those after it get moved to new locations.  The table.delete() function is expensive due to all of this moving.

The best thing to do is to use a method called “shuffle”.  This is like a deck of cards.  You shuffle the list of numbers and then simply loop over the table 1 by 1.  When you get to the end of the list, reshuffle.  I personally use this function:

https://developer.coronalabs.com/code/shufflerandomize-tables

local mynumbers = {1, 2, 3, 4, 5, 6 }

table.shuffle( mynumbers )

for i = 1, #mynumbers do

     print( mynumbers[i] )

end

thanks Rob for sharing the knowledge… how this function should work if you want to select 2 numbers form table without repeating them,

for example …

local mynumbers = {1, 2, 3, 4, 5, 6 }

x = 4

I want another two numbers (y and z) from the table but they should not equal to x and they should not be repeated.

regards

Abdul