Random Number Display Issue

Hi all,

First post here as I begin to learn coding with Codea…

The issue I am having is that I want to display random scores next to team names such as below:

Montreal - 27
Toronto - 18

And in beginning this experiment I started to just try and understand randomizing with this tool by writing the following simple code:

x = math.random(50)
y = math.random(50)

What I get is the numbers overwriting themselves with each frame redraw I am guessing. So I guess the question is, is there any way to pause the frame redraw? I am writing mostly a text based sports game, so text handling and randomization is quite important. Any ideas, or is this not able to happen because of how the tool functions?


Hi Hunter / @Munkie, welcome to the forums! You can achieve what you are aiming at by performing a test in the draw() loop on a variable flag that tells you whether to generate new numbers or not. If you only want to do it once you could put the number generation in setup() and do the text drawing in draw().

Codea works differently from many other languages - it won’t stop drawing. Think of the draw() function like the refresh rate of a television: it has to draw something x times per second.

There’s a way to change that behavior - I’m sure someone will post it - but I don’t use that mostly. But @Fred’s right - the “right” way to accomplish what you want is to, for example, make a table in your setup(), assign the scores randomly in that table - then display the content of the table when you refresh the screen.

If you aren’t doing anything with random numbers other than this, you can simply do math.randomseed(1) at the beginning of your draw loop (I do this to do things like starfields) - you’ll then get the same sequence of random numbers each time thru. It’s cheap, and will break if you generate the random numbers in a different order later, but there you go.

Thank you very much for the responses. Are tables the same as arrays? Would either of you know of a good table tutorial as it is applied in Codea?

Thanks again,

Lua arrays are actually just tables.

See http://www.lua.org/pil/index.html

and specifically http://www.lua.org/pil/11.1.html

@Munkie in Lua, arrays are just a form of table that is indexed on sequential integers starting from 1.

Here’s a brief description of arrays and tables, and common usage.

Lua Tables

-- Tables can be constructed with the form { key1 = value1, key2 = value2, ... }
-- Complex and integral keys require square brackets, plain string keys 
-- with no spaces do not need quotes

aTable = { Something = 5, ["Something Else"] = 6, [1] = 1, "x", "y" }

-- Empty table
aTable = {} 

aTable.someValue = 200

print( aTable.someValue )

-- You can also index tables with brackets

print( aTable["someValue"] ) 

aTable[5] = 50

print( aTable[5] ) 

-- table.insert inserts the value into the first unused integer key starting at 1

table.insert( aTable, 51 ) -- Inserts value 51 at position 1 in aTable

Lua Arrays

-- You can construct them with values
anArray = { 5, 4, 3, 2, 1 }

-- Or empty
anArray = {}

table.insert( anArray, 5 )
table.insert( anArray, 4 )
table.insert( anArray, 3 )
table.insert( anArray, 2 )
table.insert( anArray, 1 )

Printing a table

for k,v in pairs( aTable ) do
    print( k, v )

Thank you once again for the responses, I do appreciate the community that goes along with this application.

So, in an array based language I might do the following for a multidimensional array:

Dim myArray(100,5) as integer

for i = 1 to 100
for y = 1 to 5
myArray(i,y) = int(rnd*10)+1
next y
next i

How might I write the same code in LUA? I do not see a translation for a multidimensional array.

Thanks once again,

P.S. I cant even get the code to show correct in the forums… ugh! How to a force a CR on a line in the forums?

For multidimensional arrays, most languages (Lua included) will have you declare them as an array-of-arrays. Here’s a 10x10 array in Lua.

myArray = {}

for x = 1,10 do
    -- Initialise an empty array for each x
    myArray[x] = {} 

    for y = 1,10 do
        myArray[x][y] = x * y

Use ~~~ (three tildes) before and after your code to have it display as a code block.



Your Code


Thanks Very Much Simeon,

I am now moving forward with an understanding of how to dimension and populate arrays (tables). Also, now I know how include code snippets and have them retain their formatting.

A quick general question about the Codea editor…

I like the ability to change a number by sliding a finger across the ipad, but I have a hard time stopping on the number I need, any hints?


@Munkie - buy a bluetooth keyboard!
The codea development environment is fantastic (love the colour selector) however sometimes trying to code on the screen (especially whilst trying to view code) is just too awkward, for any serious development work on your iPad I’d say an external keyboard is a must.

@TechDojo - Thanks so much for the feedback. As I am moving along in my project and getting comfy with the syntax and possibilities I agree that it will be worth it to invest in an accessory like that.

I guess my only concern is that I develop whenever I have a spare 15 minutes, whether it is before I go to sleep, or even in the hot tub (don’t ask).

But you did say “Serious Development”… right? :slight_smile:


The targus keyboard I brought actually fits inside the slipcover I use to keep my iPad out of the sticky hands of my kids, so it’s always to hand - granted just having the abilty to fire up the pad and code is such a joy!

PS. There should be nothing serious about game development! :slight_smile:

So I went out a got a portable bluetooth keyboard. They did have the Targus, however it was a bit large for the folder I carry my iPad in. I also use my iPad in meetings for notes, email and the like.


That is the one I am going to give a try with.

Thanks for the recommendation, much appreciated.


That’s a nice looking keyboard, it’ll make a world of difference using it with Codea

A bit too late, but I can also recommend the Logitech one: http://www.logitech.com/en-roeu/tablet-accessories/keyboards/devices/keyboard-case-ipad