Log In  


Cart #30419 | 2016-10-09 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2

Sometimes, your game makes use of a randomly generated sequence, but it would be nice to smooth out some of the short-term variance in it (e.g. long stretches of not getting a long piece in Tetris). This cart showcases some algorithms you can use to generate smoother-feeling random sequences, and lets you visualize them for tetrominoes, digits, or dice rolls.

Thanks to TetrisConcept without whom I would never have learned about these techniques, and to the developers who invented them.

See the code comments for more on the implementation of each randomizer.

2


Reminds a bit of my sketch on Khan Academy here.


This is really WAY over my head, DrPete. But checking your code I can see you are doing a vast number of ways to get random values, and to get them based on conditions, restrictions, and evictions. :)

If you're curious, you might use some line charts to graph out patterns found in your methods.


 --makes a table from 0 to (to)
 function range(to)
  local array={}
  for i=1,to do
   array[i]=i-1
  end
  return array
 end
 --returns a table with index (v) removed from table (t)
 function pop(t,v)
  local array={}
  local index=1
  for i=1,#t-1 do
   if(i==v)then
    index+=1
   end
   array[i]=t[index]
   index+=1
  end
  return array
 end
 --settings
 local old=nil
 local size=10
 --makes a table from 0 to (size)
 --pops out (old) from table
 --takes out a random element from table
 function randommemory()
  if(old==nil)then
   local take=flr(rnd(size))
   old=take
   return take
  else
   local selectable=range(size)
   selectable=pop(selectable,old+1)
   local take=flr(rnd(#selectable))
   old=selectable[take+1]
   return selectable[take+1]
  end
 end
 --takes one from 0 to (size)
 --if the value is equal to (old) then repeat
 function randomloop()
  while(true)do
   local take=flr(rnd(size))
   if(take~=old)then
    old=take
    return take
   end
  end
 end
 --printing out
 for i=1,10 do
  print(randomloop())
 end

Anastasia, I am looking at your link. Are you just wanting to plot colored dots where no colored dot matches the one next to it either above, below, left, or right ?


Left and above. Below and right doesn't really matter because it loops from top left to bottom right. It would matter if the cells were placed randomly in order. You would need to push {x:-1,y:-1},{x:1,y:-1} into neighbourPositions for diagonal checking.


This is not that difficult a task I think. Lemme take a crack at it. :)

Cart #30505 | 2016-10-10 | Code ▽ | Embed ▽ | No License

17-lines of code or 87 tokens.
I can add diagonal checking too if you like ...


That's pretty good.
It would be

(x==0 or scr[i-1]!=c) and scr[i-15]!=c and (y==0 or scr[i-16]!=c) and scr[i-17]!=c

right? Or even shorter for diagonal checking?


Hmm ... I think that's missing something ... Yes, you need scr[i-17]!=c as well.

Here is the update, now checks for all diagonals and will not choose matching colors if they do.

Cart #30528 | 2016-10-10 | Code ▽ | Embed ▽ | No License



[Please log in to post a comment]