Shake Problem!

Hi

The code underneath works perfectly for my project…shake gesture and a random image is selected from a selection of 50. So far, so good! However does anyone know how I can stop the first random image always being number 1.png?

I have tried playing around e.g. changing the number 1 in the

local r = math.random(1,#img); line but without success. I would like the random selection to be random from the first shake not the second! Any ideas welcome.

Thanks

Bob

module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


local background = display.newImage (“shake.png”)
localGroup:insert(background)
–> This sets the background

local img = { “1.png”, “2.png”, “3.png”, “4.png”, “5.png”, “6.png”, “7.png”,“8.png”,“9.png”,“10.png”,“11.png”,
“12.png”,“13.png”,“14.png”,“15.png”,“16.png”,“17.png”,“18.png”,“19.png”,“20.png”,“21.png”,“22.png”,“23.png”,“24.png”,“25.png”,“26.png”,“27.png”,“28.png”,“29.png”,“30.png”,“31.png”,“32.png”,“33.png”,“34.png”,“35.png”,“36.png”,“37.png”,“38.png”,“39.png”,“40.png”,“41.png”,“42.png”,“43.png”,“44.png”,“45.png”,“46.png”,“47.png”,“48.png”,“49.png”,“50.png”,};

local function onShake(event)
if event.isShake then
local r = math.random(1,#img);
local image = display.newImage(img[r], 0, 0);
end

return true;
end

Runtime:addEventListener(“accelerometer”, onShake);
return localGroup
end [import]uid: 45029 topic_id: 10422 reply_id: 310422[/import]

You need to first set a new seed for the random number generator - look up math.randomseed()
To make sure it is different every time, you could set the seed from the system time, something like this:

math.randomseed ( os.time ( ) )

Hi snarla

Thanks, this worked perfectly. As someone who is totally new to Corona AND coding, my experience of these forums has been an extremely positive one. If someone had said to me
3 or 4 weeks ago that I would be creating my own apps, I would have laughed, designing them…of course, actually involved in the coding of them…no way! Thanks again.

Regards
Bob [import]uid: 45029 topic_id: 10422 reply_id: 38022[/import]