Hello again))
Is there any way to ensure that math.random function will always get different numbers?
i have this set up, but images often repeated, i'd like them to be different every time and not match
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | math.randomseed( os.time() ) local shipTable = { {img = "rocketShip1.png", color = "green"}, {img = "rocketShip2.png", color = "blue"}, {img = "rocketShip3.png", color = "red"} } maxImages = 3 t = {} for many=1, maxImages do local i = math.random(#shipTable) t[many] = display.newImage(shipTable[i].img) t[many].color = shipTable[i].color t[many].x = math.random(50,300) t[many].y = math.random(50,300) local function onTouch(event) print(image.color) end t[many]:addEventListener("touch", onTouch) end |
Nope. But you can save the last math.random number and check it if it's diffrent.
something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | local lastRandomNumber = 0 local function random(min,max) local r = math.random(min,max) if r==lastRandomNumber then return random(min,max,oldValue) else lastRandomNumber = r return r end end for i=0,20 do print(random(1,2)) end |
i tried it with my function and still object came out two of them same...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | local lastRandomNumber = 0 local function random(min,max) local r = math.random(min,max) if r==lastRandomNumber then return random(min,max,oldValue) else lastRandomNumber = r return r end end local shipTable = { {img = "rocketShip1.png", color = "green"}, {img = "rocketShip2.png", color = "blue"}, {img = "rocketShip3.png", color = "red"} } maxImages = 3 t = {} for many=1, maxImages do local i = random(1,3) t[many] = display.newImage(shipTable[i].img) t[many].color = shipTable[i].color t[many].x = math.random(50,300) t[many].y = math.random(50,300) end |
you didn't call the random function, but math.random :)
i did called "random" function in line 27: local i = random(1,3)
...
Oh right. Sorry I've just skipped through your code. hmm.. That's odd.
Could you zip a little sample for me?
Strange. Maybe because it's looping before it can create the new number.. Dunno..
You could also make a table from and fill it and afterwards shuffle it!
Here are 2 shuffle functions for you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | local function randomizeArray(arr) for i = #arr, 2, -1 do -- backwards local r = math.random(i) -- select a random number between 1 and i arr[i], arr[r] = arr[r], arr[i] -- swap the randomly selected item to position i end return arr end local function randomizeArrayAlt(arr) local len = #arr for i = 1, len, 1 do -- backwards local ran = math.random(1,len) -- select a random number between 1 and i local char = arr[ran] arr[ran] = arr[i] arr[i] = char end return arr end |
thanks for shuffling functions, it will come in handy
thanks for help, you're awesome)
by the way, do you have a skype account?
No Problem.
Too bad I couldn't fix your problem :/ Damnit!
Sure: search for xxxfanta ;)
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Best Solution for Game
Accessing remote Android test devices
Um... "ReferenceTable overflow (max=512) "
system.vibrate() causes crash
Corona Debugger
PDF Support
Holiday Advent Calendar HD Lite Released
Pattern matching help.
(Re-)Launching CPM from within BBEdit