# Writing A Scramble Generator In Lua for a Timing Program



## dChan (Jun 19, 2008)

Lately I have been learning the Lua programming language, particularly I have been learning to program for the Sony PSP. I have created several small programs so far for the PSP and am actually in the middle of a large project but I have take off from the project to finish learning my last two basic Lua lessons (timers and collision). Since I just finished learning how to create, control, and use timers in Lua (I started learning this morning and ended a couple of hours ago) I was eager to create a timing program for Rubik's cube solving. I have the basics down right now which include the ability to display milliseconds up to the third point and the feature to save your time(whether it is your best time or not). Later I am going to add the ability to switch between different categories of solving like speed, one-handed, and blindfolded. This brought me to realizing that I need to figure out how to generate scrambles and since I absolutely have no prior knowledge of writing scramble generators nor any prior coding knowledge at all I really need some help with this. Would it be possible for someone to help me understand how to generate scrambles? If you know Lua that would be great but I guess I just really need to understand the concept behind how it works because I have no clue at all how it does. It would be nice if you could give some examples too.

Thanks a bunch,
-dChan


----------



## JBCM627 (Jun 19, 2008)

see comments in src code: http://www.thewonderidiot.net/timer/scramble.html

And WCA forum, particularly: http://www.worldcubeassociation.org/forum/viewtopic.php?f=4&t=449#p2188


----------



## linkmaster03 (Jun 19, 2008)

I made a scrambler for a graphing calculator. Basically you generate one random number, 1-6 (A), and another 0-2 (B) (or 1-3 for that matter). If A=1, show F. If A=2, show B. If B=0, do nothing. If B=1, show '. If B=3, show 2. 

Then it loops back to the beginning generating new numbers. There's a basic random scrambler. But you need to add detection for F F, F F', F F2. Also prevent R L R, R L R', etc. There you need to use your imagination and logic.


----------



## dChan (Jun 19, 2008)

I understand how the logic of it all works now but the problem is: how do you generate random numbers? I'm completely clueless there. I found a snippet for generating random movement so would I be able to translate that to generating random numbers (just cut off all the parts that translate the numbers into movements?)?


----------



## linkmaster03 (Jun 19, 2008)

Almost every programming language has a function for generating random numbers. Google it, as nobody here knows Lua I bet.


----------



## ShadenSmith (Jun 19, 2008)

You'll want to use math.random(max)

It will generate a random number between 1 and max.


----------



## JBCM627 (Jun 19, 2008)

There should be a Lua generator function. In javascript, its Math.random(), java there is nextInt(), c++ there is srand and rand, and there are similar functions in every language I am aware of.

In Lua, here is a reference: http://www.lua.org/pil/18.html

It looks like you will want to seed it: math.randomseed(os.time())
then you can call math.random. In most languages, this generates a number in the interval [0,1), so you can do something like math.floor(math.random*6), will give you a number from 0 to 5.


----------



## ShadenSmith (Jun 19, 2008)

math.randomseed(os.time())
math.random(6)


That will give you a random number between 1 and 6.


----------



## dChan (Jun 19, 2008)

Thanks for your help everyone. I think just from learning that random generator function I should be able to figure out how to put the scramble generator together on my own. Thanks a lot, this will definitely help me give my timer program a better bunch of features.


----------



## Johannes91 (Jun 19, 2008)

linkmaster03 said:


> Nobody here knows Lua I bet.


I do, for one. I've made a few levels for Enigma.


----------



## linkmaster03 (Jun 19, 2008)

I don't, and most people here don't. I was just mentioning he would find answers much faster with Google.


----------



## dChan (Jun 21, 2008)

Well I actually would not bbe surprised if quite a few people here know Lua(it is actually one of the most popular languages, check lua.org). But you are right that I probably would be able to find answers faster through google but the problem was that I had no idea where to begin and since you guys are all the experts I thought I would try here.

Thanks for all your help guys. I am actually going to include the scramble generator in a future version of my program. I already released the first version to the public. Thanks for that random generator code! That will help lot.


----------

