Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Help: Random number that doesn't repeat
Started by Jam Paraiso Jul 27 2013 09:32 AM

* * * - - 1 votes
6 replies to this topic
random random question math.random() question randomize

Best Answer Rob Miracle , 27 July 2013 - 07:49 PM

j = math.random(i)  -- Notice I changed this.....
t[i], t[j] = t[j], t[i]  -- << this one? what does it do?
 

 

Lua allows you to set multiple values at once.  In this case, it's basically swapping the two values:

 

t -- is the table reference passed to the function.

i and j are indexes into the table.

 

In this case, it's simply taking t[j] and putting it into t[i], while at the same time taking t[i] and putting it in t[j].   This seems weird because you would think it would overwrite each other.  In traditional languages you would do:

 

local tmp = t[i]

t[i] = t[j]

t[j] = tmp

 

But this Lua short cut bypasses it.

 

I also noticed that the code is just calling a function called "random", which I'm guessing in the code I copied that from, there was a line above that's not in the sample that reads:

 

local random = math.random

 

which localizes random so it's not a table lookup to find it.  I've updated the code to simply call math.random instead.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Jam Paraiso

[GLOBAL: userInfoPane.html]
Jam Paraiso
  • Enthusiast

  • 98 posts
  • Corona SDK

Hello guys, 

 

I want to know if how can I do a random number using math.random() or other ways, that if it sees that the number has been already returned from the previous number randomization, it will do math.random again.

 

Let say I do math.random(1, 10) then on the first loop it returns 2,on second loop it returns 1, on the third loop it returns 2 again, since 2 is already given it will just loop again for another number.

 

Because I'm planning to have a game that will give the player a random set of questions, then every time the player answers that particular question correctly, I will marked that question and will not be ask again from the player.

 

I do get the logic, but I can't express it in code :(

 

What is the best way to do this? I've been doing it for like 3 hours but still no luck.

 

Your help will be much appreciated

 

Thanks in advance,

 

Jam



[TOPIC: post.html]
#2

ksan

[GLOBAL: userInfoPane.html]
ksan
  • Corona Geek

  • 2,795 posts
  • Corona SDK

Check the following thread : http://forums.coronalabs.com/topic/37122-non-duplicate-randomiser/?hl=random

 

Almost same question was discussed and I recall there was a great solution proposed. Hope it works for you as well. 



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,922 posts
  • Enterprise

What you want to do is called "Shuffling".  Its like a deck of cards.  You have a fixed number and can only use one once.  If you search for "shuffle table" you will find many different ways of shuffling your data.  Now you do have to set up a table with your numbers like:

 

local myNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

 

Then you shuffle the table and it gets randomized.  Then you just iterate over the table in your for loop getting the numbers.



[TOPIC: post.html]
#4

Jam Paraiso

[GLOBAL: userInfoPane.html]
Jam Paraiso
  • Enthusiast

  • 98 posts
  • Corona SDK

firstly thanks for the reply,

 

I searched for the shuffle table and i found your post

http://forums.coronalabs.com/topic/27576-how-to-randomize-scene-with-out-repeat/?hl=%2Bshuffle+%2Btable#entry148337

this does work,  thanks for that :)

but this time I want to see in the terminal the shuffled items.

 

and If you don't bother would explain some of the codes in you syntax:

 

local sceneNames = {"scene1", "scene2", "scene3", "scene4"}

 

local function shuffle(t)

local iterations = #t
local j

for i = iterations, 2, -1 do
j = math.random(i)
t[i], t[j] = t[j], t[i]  --<< this one? what does it do?
end


end


shuffle(sceneNames)

 

thank you very much for your patience and time.

 

Jam



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,922 posts
  • Enterprise

  Best Answer

j = math.random(i)  -- Notice I changed this.....
t[i], t[j] = t[j], t[i]  -- << this one? what does it do?
 

 

Lua allows you to set multiple values at once.  In this case, it's basically swapping the two values:

 

t -- is the table reference passed to the function.

i and j are indexes into the table.

 

In this case, it's simply taking t[j] and putting it into t[i], while at the same time taking t[i] and putting it in t[j].   This seems weird because you would think it would overwrite each other.  In traditional languages you would do:

 

local tmp = t[i]

t[i] = t[j]

t[j] = tmp

 

But this Lua short cut bypasses it.

 

I also noticed that the code is just calling a function called "random", which I'm guessing in the code I copied that from, there was a line above that's not in the sample that reads:

 

local random = math.random

 

which localizes random so it's not a table lookup to find it.  I've updated the code to simply call math.random instead.



[TOPIC: post.html]
#6

Jam Paraiso

[GLOBAL: userInfoPane.html]
Jam Paraiso
  • Enthusiast

  • 98 posts
  • Corona SDK

thanks Rob for the explanations.

 

You really are a big help :)



[TOPIC: post.html]
#7

toga

[GLOBAL: userInfoPane.html]
toga
  • Contributor

  • 103 posts
  • Corona SDK

Here is also a very interesting LUA example how to generate "N different random numbers from the set 1..M"

 

http://lua-users.org/wiki/RandomSample

 

To be honest i do not really understand the code but it works great :-)




[topic_controls]
[/topic_controls]