random position in grid but single value

hi,

I use a grid to position element and i use a random function to put this element.

My question is how to have single value gX or gY to be sure that some element are not at the same position?

the problem is that “numberelement” could be change between my levels. 

local grid={} .... local function randomGrid() --to have random number to my grid        gXc=math.round(mathr(1,row))      gYc=math.round(mathr(1,line))      gX=grid[gYc][gXc].x      gY=grid[gYc][gXc].y end

for i=1, numberElement do--numberElement = 5 or much... randomGrid() transition.to(element,{time=400,x=grid[gY][gX].x, y=element,x=grid[gY][gX].x}) end

hi, there are a lot of solutions for your problem.

you can check:

http://codereview.stackexchange.com/questions/61338/generate-random-numbers-without-repetitions

or

http://stackoverflow.com/questions/23176467/c-generate-random-numbers-without-repetition

the teory is all there.

Thanks, but it is C language. I’m starting lua from 2 years. I haveno ability to reproduce that with lua. A  solution is so generate pre-random table numbers and use them. Moreover it take less memory…No?

Hi. There are some threads about “shuffling”, such as this one.

Then you could just increment an index and read from it:

local card\_index = 0 -- "card" just to follow the "shuffle" terminology local deck = shuffle\_deck() -- same with "deck" :) local function GetIndex () card\_index = card\_index + 1 return deck[card\_index] end

and do something if you run out of “cards”.

An alternative is to just grab the indices at random, but then you have to remove them so you won’t get them again:

local deck = {} for i = 1, NumberOfCards do -- this is your per-level number, of course deck[i] = i end local function GetIndex () local card\_index = deck[math.random(#deck)] -- this uses the current size of the deck table.remove(deck, card\_index) return card\_index end

You could make separate lists for x and y (although you’ll have problems if the grid isn’t square), or use a flat index in between. For a grid you could use these to convert to and from a flat index:

function GridIndex (x, y) return (y - 1) \* NumberOfColumns + x -- this is your grid width end function GridXY (index) local x = (index - 1) % NumberOfColumns + 1 local y = (index - x) / NumberOfColumns + 1 -- just reverse the GridIndex() formula end

You might want to work out some examples by hand to see how the indices work.

thanks, 

just a question, your shuffle_deck is the same function than Rob ? in this case I’v got this error “table.shuffle() expected a table, got nil”

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

?

Well, I didn’t point out one, but yes, that’s a perfectly good one to use.

I wrote in a pretty generic way, but basically you could change my earlier code to

local deck = {} -- same with "deck" :) shuffleTable(deck)

I hope the cards terms didn’t make it more confusing.  :slight_smile:

lua is derived from C, it should be pretty easy to understand and convert c code to lua code.

if you read the links i put here you will see that there are a lot of approaches to the problem, depends of the number of elements you want to “shuffle”. all the solutions have their pros and cons, really depend on the size of your table to randomize.

but this kinda of problems are not programing problems, are algorithmic problems. you really should learn algorithmic first before going to any programming language, you will save alot of time in the long way. this are lvl 1 problems, if you are having trouble in this after 2 years in a language, the problem is not in the language you are learning, are in the base knowledge you have. don’t interpert this as a critic, but as a suggestion of the road that you should go.

regards,

Carlos.

Hello,
 

You have offended my ego…and I thank you :slight_smile: because I found how it works and I post here the solution, it may be able to help others.

Good day to everyone.

local numberOfelement = 60 -- only put here your value local xScope = 5 --put for example the number of your row in your grid local Yscope = 10 --put for example the number of your line in your grid --dont touch this  local random = {} local xPosArray = {} local yPosArray = {} local randX,randY = 0,0 local xExists,yExists = 0,0 local function randomize(i)   randX = math.random(1,xScope)   randY = math.random(1,yScope)   if(i==1)then     xPosArray[i] = randX     yPosArray[i] = randY   else     xExists = table.indexOf( xPosArray, randX )     yExists = table.indexOf( yPosArray, randY )     if(xExists~=nil and yExists~=nil and xExists==yExists)then       print("Occuped")       randomize(i)     else       print("ok")       xPosArray[i] = randX       yPosArray[i] = randY     end   end   return randX,randY end for i=1,numberOfelement do   randomize(i)   print(randX,"randX",randY, "randY")   --you can make for example : transition.to(myobject, {time=500, x=randX, y=randY })   --numberOfelement = number of myobject end

I would have to argue the point that Lua is derived from C. Lua seems to me to be derived from BASIC more than any thing.  C is very syntax heavy using curly braces  for block defintions intead of words.  For instance:

if (!isMusicOn) {      println(stdout,"Music is off"); } else {      println(stdout,"Music is on"); }

vs.

if not isMusicOn then      print( "Music is off" ) else      print( "Music is on" ) end

Yes, they share keywords like “if”, “for”, but so does every language. Functions generally put things in () in every language too, but for Lua, the () are optional if the parameters are a single inline table.

C supports auto-increment and auto-decrement variables which Lua does not, much like BASIC.

For someone who has learned an easy to learn, low syntax language like Lua, trying to read C can be terrifying. For someone who knows Java, JavaScript, ActionScript, etc. reading C isn’t hard because they are all derived from C.

There are multiple ways to shuffle an array and they are all about the same. I prefer the one posted above that has my name on it, though it’s not mine, it’s just the one I use. It’s something that every one of us should have in our arsenal of code.

Rob

It’s scattered around a bit, but in their HOPL paper the Lua authors cite (at least) CLU, Icon, Snobol, Awk, and Perl, in many cases borrowing just one or two features. They also claim significant influence from Scheme.

lua is a procedural and imperative language, like C. the way of thinking is exactly the same. of course the way of creating the code is diferent. but if you study the algorithmit behind it its pretty easy to port from C to any other procedural and imperative language.

it’s not like OOP languages like Java, that the approach of the problem is different. or like Prolog which is a declarative language.

hi, there are a lot of solutions for your problem.

you can check:

http://codereview.stackexchange.com/questions/61338/generate-random-numbers-without-repetitions

or

http://stackoverflow.com/questions/23176467/c-generate-random-numbers-without-repetition

the teory is all there.

Thanks, but it is C language. I’m starting lua from 2 years. I haveno ability to reproduce that with lua. A  solution is so generate pre-random table numbers and use them. Moreover it take less memory…No?

Hi. There are some threads about “shuffling”, such as this one.

Then you could just increment an index and read from it:

local card\_index = 0 -- "card" just to follow the "shuffle" terminology local deck = shuffle\_deck() -- same with "deck" :) local function GetIndex () card\_index = card\_index + 1 return deck[card\_index] end

and do something if you run out of “cards”.

An alternative is to just grab the indices at random, but then you have to remove them so you won’t get them again:

local deck = {} for i = 1, NumberOfCards do -- this is your per-level number, of course deck[i] = i end local function GetIndex () local card\_index = deck[math.random(#deck)] -- this uses the current size of the deck table.remove(deck, card\_index) return card\_index end

You could make separate lists for x and y (although you’ll have problems if the grid isn’t square), or use a flat index in between. For a grid you could use these to convert to and from a flat index:

function GridIndex (x, y) return (y - 1) \* NumberOfColumns + x -- this is your grid width end function GridXY (index) local x = (index - 1) % NumberOfColumns + 1 local y = (index - x) / NumberOfColumns + 1 -- just reverse the GridIndex() formula end

You might want to work out some examples by hand to see how the indices work.

thanks, 

just a question, your shuffle_deck is the same function than Rob ? in this case I’v got this error “table.shuffle() expected a table, got nil”

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

?

Well, I didn’t point out one, but yes, that’s a perfectly good one to use.

I wrote in a pretty generic way, but basically you could change my earlier code to

local deck = {} -- same with "deck" :) shuffleTable(deck)

I hope the cards terms didn’t make it more confusing.  :slight_smile:

lua is derived from C, it should be pretty easy to understand and convert c code to lua code.

if you read the links i put here you will see that there are a lot of approaches to the problem, depends of the number of elements you want to “shuffle”. all the solutions have their pros and cons, really depend on the size of your table to randomize.

but this kinda of problems are not programing problems, are algorithmic problems. you really should learn algorithmic first before going to any programming language, you will save alot of time in the long way. this are lvl 1 problems, if you are having trouble in this after 2 years in a language, the problem is not in the language you are learning, are in the base knowledge you have. don’t interpert this as a critic, but as a suggestion of the road that you should go.

regards,

Carlos.

Hello,
 

You have offended my ego…and I thank you :slight_smile: because I found how it works and I post here the solution, it may be able to help others.

Good day to everyone.

local numberOfelement = 60 -- only put here your value local xScope = 5 --put for example the number of your row in your grid local Yscope = 10 --put for example the number of your line in your grid --dont touch this  local random = {} local xPosArray = {} local yPosArray = {} local randX,randY = 0,0 local xExists,yExists = 0,0 local function randomize(i)   randX = math.random(1,xScope)   randY = math.random(1,yScope)   if(i==1)then     xPosArray[i] = randX     yPosArray[i] = randY   else     xExists = table.indexOf( xPosArray, randX )     yExists = table.indexOf( yPosArray, randY )     if(xExists~=nil and yExists~=nil and xExists==yExists)then       print("Occuped")       randomize(i)     else       print("ok")       xPosArray[i] = randX       yPosArray[i] = randY     end   end   return randX,randY end for i=1,numberOfelement do   randomize(i)   print(randX,"randX",randY, "randY")   --you can make for example : transition.to(myobject, {time=500, x=randX, y=randY })   --numberOfelement = number of myobject end

I would have to argue the point that Lua is derived from C. Lua seems to me to be derived from BASIC more than any thing.  C is very syntax heavy using curly braces  for block defintions intead of words.  For instance:

if (!isMusicOn) {      println(stdout,"Music is off"); } else {      println(stdout,"Music is on"); }

vs.

if not isMusicOn then      print( "Music is off" ) else      print( "Music is on" ) end

Yes, they share keywords like “if”, “for”, but so does every language. Functions generally put things in () in every language too, but for Lua, the () are optional if the parameters are a single inline table.

C supports auto-increment and auto-decrement variables which Lua does not, much like BASIC.

For someone who has learned an easy to learn, low syntax language like Lua, trying to read C can be terrifying. For someone who knows Java, JavaScript, ActionScript, etc. reading C isn’t hard because they are all derived from C.

There are multiple ways to shuffle an array and they are all about the same. I prefer the one posted above that has my name on it, though it’s not mine, it’s just the one I use. It’s something that every one of us should have in our arsenal of code.

Rob

It’s scattered around a bit, but in their HOPL paper the Lua authors cite (at least) CLU, Icon, Snobol, Awk, and Perl, in many cases borrowing just one or two features. They also claim significant influence from Scheme.