help with probability random array

can someone give sample to do random array with probability?
let say i have:

local data = {1, 2, 3, 4, 5, 6}  
-- how i can randomize it with custom percentage for appearing data[i]   

someone please some advice…:slight_smile: [import]uid: 33180 topic_id: 8165 reply_id: 308165[/import]

i wanna try to create roulette game…so please some advice…:slight_smile: [import]uid: 33180 topic_id: 8165 reply_id: 29090[/import]

You can do this

math.randomseed(os.time())
local i = math.random(1,36)
local number = data[i]

[import]uid: 11809 topic_id: 8165 reply_id: 29099[/import]

oh, ok, thanks…but how about the percentage…in case, i wanna set how often the number will show up or chosen?

like 1 --> maybe 2%
[import]uid: 33180 topic_id: 8165 reply_id: 29100[/import]

no more clue??? *~* [import]uid: 33180 topic_id: 8165 reply_id: 29122[/import]

You could always weight your array, for example if you wanted 1 to show up twice as often:

local data = {1, 1, 2, 3, 4, 5, 6}  

Then 1 should show up twice as often as the other numbers :slight_smile: [import]uid: 17049 topic_id: 8165 reply_id: 29179[/import]

hmm…ok, thanks,rharby… [import]uid: 33180 topic_id: 8165 reply_id: 29272[/import]

Stacking the array like rharby suggested is the way I would do it. It will mean a little up front work to calculate the percentages of all the numbers, but other than that, it should be easy.

I would personally visit a local college to find an über math geek to figure it out in his head for you. [import]uid: 5317 topic_id: 8165 reply_id: 29283[/import]

thanks, mike4… if the application works well, I’ll share the code with u all…:slight_smile: [import]uid: 33180 topic_id: 8165 reply_id: 29284[/import]

local initialSpeed = 20  
local brakes = 0.5  
local speed = {initialSpeed, initialSpeed}  
local stopping = {0,0}  
local touched = 0  
  
local area = { "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1",  
  
 "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2",   
 "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2",   
  
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
  
 "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4",   
 "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4",   
 "area4", "area4", "area4", "area4", "area4",   
  
 "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5",  
 "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5",  
 "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5",  
 "area5", "area5", "area5", "area5", "area5",  
  
 "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6",   
  
 "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7",   
 "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7",   
 "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7",   
  
 "area8", "area8", "area8", "area8", "area8",   
  
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
  
 "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10",   
 "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10",   
 "area10", "area10", "area10", "area10", "area10"  
 }  
  
local function RemoveAll ()  
Runtime:removeEventListener( "enterFrame", gameLoop )  
Runtime:removeEventListener( "enterFrame", applyBrakes )  
Runtime:removeEventListener("enterFrame", gameResult)  
print("completed")  
end  
---------------------------------------------------------------------------------------------  
----------------------------------------Game Result------------------------------------------  
---------------------------------------------------------------------------------------------  
local function checkArea(n)  
 for n = math.random(1, 100), #area, 100 do  
 areaCheck = area[n]  
 end  
 check = areaCheck  
end  
  
local function gameResult()  
 local score  
 if arrow.rotation \> 0 and arrow.rotation \<= 36 then  
 score = "area1"  
 elseif arrow.rotation \> 36 and arrow.rotation \<= 72 then  
 score = "area2"  
 elseif arrow.rotation \> 72 and arrow.rotation \<= 108 then  
 score = "area3"  
 elseif arrow.rotation \> 108 and arrow.rotation \<= 144 then  
 score = "area4"  
 elseif arrow.rotation \> 144 and arrow.rotation \<= 180 then  
 score = "area5"  
 elseif arrow.rotation \> 180 and arrow.rotation \<= 216 then  
 score = "area6"  
 elseif arrow.rotation \> 216 and arrow.rotation \<= 252 then  
 score = "area7"  
 elseif arrow.rotation \> 252 and arrow.rotation \<= 288 then  
 score = "area8"  
 elseif arrow.rotation \> 288 and arrow.rotation \<= 324 then  
 score = "area9"  
 else --if arrow.rotation \> 324 and arrow.rotation \<= 360 then  
 score = "area10"  
 end  
 globalScore = score  
end  
  
---------------------------------------------------------------------------------------------  
---------------------------------------Game Looping------------------------------------------  
---------------------------------------------------------------------------------------------  
  
local function applyBrakes(event)  
 for i = 1, 2 do  
 if stopping[i] == 1 then  
 speed[i] = speed[i] - brakes  
 Runtime:removeEventListener("enterFrame", checkArea)  
 Runtime:addEventListener("enterFrame", gameResult)  
 gameResult()  
 print ("area = "..check)  
 print("globalscore = "..globalScore)  
 if (globalScore == check) == false then  
 if speed[i] \<= 0 then  
 speed[i] = speed[i] + 0.05  
 end  
 print("do nothing")  
 else  
 speed[i] = 0  
 end   
 end  
 end  
end  
  
local function applyMotion(i)  
 if arrow.rotation \>= 360 then  
 arrow.rotation = arrow.rotation - (360) + speed[i]  
 topboard.rotation = arrow.rotation  
 else  
 arrow.rotation = math.floor (arrow.rotation + speed[i])  
 topboard.rotation = arrow.rotation  
 end  
end  
  
local function gameLoop()  
 for i = 1, 2 do  
 applyMotion(i)  
 break  
 end  
end  
  
---------------------------------------------------------------------------------------------  
-----------------------------------------Button----------------------------------------------  
---------------------------------------------------------------------------------------------  
local buttonStart = ui.newButton {  
 default = "Images/button.png",  
 over = "Images/button.png",  
 id = "Start",  
 text = "start",  
 font = native.systemFontBold,  
 textColor = {0,0,0, 255},  
 size = 20,  
 align = "center"  
 }  
 buttonStart.x = W   
 buttonStart.y = H + 150  
  
local buttonStop = ui.newButton {  
 default = "Images/button.png",  
 over = "Images/button.png",  
 id = "Stop",  
 text = "Stop",  
 font = native.systemFontBold,  
 textColor = {0,0,0, 255},  
 size = 20,  
 align = "center"  
 }  
 buttonStop.x = W   
 buttonStop.y = H + 150  
 buttonStop.isVisible = false  
  
local buttonSpin = ui.newButton {  
 default = "Images/button.png",  
 over = "Images/button.png",  
 id = "Spin",  
 text = "Spin",  
 font = native.systemFontBold,  
 textColor = {0,0,0, 255},  
 size = 20,  
 align = "center"  
 }  
 buttonSpin.x = W   
 buttonSpin.y = H + 150  
 buttonSpin.isVisible = false  
---------------------------------------------------------------------------------------------  
--------------------------------------Button Funtion-----------------------------------------  
---------------------------------------------------------------------------------------------  
  
-- local function spinGame (event)  
 -- Runtime:addEventListener( "enterFrame", gameLoop )  
 -- Runtime:addEventListener( "enterFrame", checkArea )  
 -- buttonStop.isVisible = true  
 -- buttonSpin.isVisible = false  
 -- if event.phase == "began" then  
 -- touched = touched + 1  
 -- if stopping[2] == 1 then  
 -- speed = { initialSpeed, initialSpeed}  
 -- stopping = {0, 0} -- 0 = false, 1 = stopping, 2 = stopped  
 -- touched = 0  
 -- return  
 -- end  
  
 -- if (touched \>= 2) then  
 -- return  
 -- end  
 -- end   
-- end  
  
local function stopGame (event)  
 Runtime:addEventListener( "enterFrame", applyBrakes )  
 buttonStop.isVisible = false  
 buttonSpin.isVisible = true  
 for i = 1, 2, 1 do  
 if stopping[i] == 0 then  
 stopping[i] = 1  
 stopping[i+1] = 1  
 break  
 end  
 end  
end  
  
local function startGame (event)  
 buttonStart.isVisible = false  
 buttonStop.isVisible = true  
 Runtime:addEventListener( "enterFrame", gameLoop )  
 Runtime:removeEventListener( "enterFrame", gameResult)  
 Runtime:addEventListener( "enterFrame", checkArea )  
end  

this is my code…
but this got some error when the arrow rotation and area check are not match, the arrow will keep spinning…i dunno how to solve it…
i wanna make it like
after user clicked stop button

if score = check
then the arrow.rotation will stop slowly to the area…

but if the arrow.rotation speed is 0 but it is still not in the area, i wanna make it rotate on one time to the area…can someone help me…:slight_smile:
[import]uid: 33180 topic_id: 8165 reply_id: 29300[/import]

I am not sure, that i understand your game. Sound like a spinning wheel or something.

Are there two rotating objects? the array and then the topboard?
Can you post an image or a simple sketch?

You have two tables speed and stopping with two fields. I assume thats for the two objects?
In the stopGame-function it seems, that you want to set both fields to one?

[lua] for i = 1, 2, 1 do
if stopping[i] == 0 then
stopping[i] = 1
stopping[i+1] = 1
break
end
end[/lua]

If i is 2 and stopping[2] is zero, than you put stopping[2] to one, but also a stopping[3] and not one. Is that intended? [import]uid: 42078 topic_id: 8165 reply_id: 29380[/import]

yes, it’s spinning wheel,

the topboard, i just tried to make it a bit look nice so i make it spins as the arrow…

hmmm, table speed and stopping, it’s for one object “arrow”…
“If i is 2 and stopping[2] is zero, than you put stopping[2] to one,” ----> yes,that is…
and it’s helping me to create condition for brakes the speed…
[import]uid: 33180 topic_id: 8165 reply_id: 29426[/import]

this is my latest code

module(...,package.seeall)  
  
local ui = require ("ui")  
require "sqlite3"  
  
local dbPath = system.pathForFile ("database.sqlite", system.DocumentsDirectory)  
local db = sqlite3.open(dbPath)  
---------------------------------------------------------------------------------------------  
----------------------------------------Interface--------------------------------------------  
---------------------------------------------------------------------------------------------  
local localGroup = display.newGroup ()  
  
local W = display.contentWidth/2  
local H = display.contentHeight/2  
  
local background = display.newImage("Images/background.png")  
 background.x = W  
 background.y = H  
  
local board = display.newImage("Images/board.png")  
 board.x = W  
 board.y = H - 100  
  
local arrow = display.newImage("Images/arrow.png")  
 arrow:setReferencePoint(display.CenterReferencePoint)  
 arrow:scale (.4, .65)  
 arrow.x = W  
 arrow.y = H - 100  
---------------------------------------------------------------------------------------------  
-------------------------------------Variable Function---------------------------------------  
---------------------------------------------------------------------------------------------  
local initialSpeed = 30  
local brakes = 2  
local speed = {initialSpeed, initialSpeed, initialSpeed, initialSpeed}  
local stopping = {0, 0, 0, 0}  
local touched = 0  
  
local area = { "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1", "area1",   
 "area1", "area1", "area1", "area1", "area1",  
  
 "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2",   
 "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2", "area2",   
 "area2", "area2", "area2", "area2", "area2",   
  
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
 "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3", "area3",   
  
 "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4",   
 "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4", "area4",   
 "area4", "area4", "area4", "area4", "area4",   
  
 "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5",  
 "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5",  
 "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5", "area5",  
 "area5", "area5", "area5", "area5", "area5",  
  
 "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6",  
 "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6", "area6",  
  
 "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7",   
 "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7",   
 "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7", "area7",   
  
 "area8", "area8", "area8", "area8", "area8",  
  
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9", "area9",   
 "area9", "area9", "area9", "area9", "area9",  
  
 "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10",   
 "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10", "area10",   
 "area10", "area10", "area10", "area10", "area10",  
 }  
  
---------------------------------------------------------------------------------------------  
----------------------------------------Game Result------------------------------------------  
---------------------------------------------------------------------------------------------  
local function checkArrow()  
 local score  
 if arrow.rotation \> 0 and arrow.rotation \<= 36 then  
 score = "area1"  
 elseif arrow.rotation \> 36 and arrow.rotation \<= 72 then  
 score = "area2"  
 elseif arrow.rotation \> 72 and arrow.rotation \<= 108 then  
 score = "area3"  
 elseif arrow.rotation \> 108 and arrow.rotation \<= 144 then  
 score = "area4"  
 elseif arrow.rotation \> 144 and arrow.rotation \<= 180 then  
 score = "area5"  
 elseif arrow.rotation \> 180 and arrow.rotation \<= 216 then  
 score = "area6"  
 elseif arrow.rotation \> 216 and arrow.rotation \<= 252 then  
 score = "area7"  
 elseif arrow.rotation \> 252 and arrow.rotation \<= 288 then  
 score = "area8"  
 elseif arrow.rotation \> 288 and arrow.rotation \<= 324 then  
 score = "area9"  
 else --if arrow.rotation \> 324 and arrow.rotation \<= 360 then  
 score = "area10"  
 end  
 globalScore = score  
end  
  
local function checkArea(event)  
 for n = math.random(1, 100), #area, 100 do  
 areaCheck = area[n]  
 end  
 check = areaCheck   
 end  
  
local function completed ()  
 if check == "area1" then  
 db:exec[[UPDATE tb\_prize SET prize = "20" WHERE id = 1]]  
 elseif check == "area2" then  
 db:exec[[UPDATE tb\_prize SET prize = "70" WHERE id = 1]]  
 elseif check == "area3" then  
 db:exec[[UPDATE tb\_prize SET prize = "30" WHERE id = 1]]  
 elseif check == "area4" then  
 db:exec[[UPDATE tb\_prize SET prize = "80" WHERE id = 1]]  
 elseif check == "area5" then  
 db:exec[[UPDATE tb\_prize SET prize = "40" WHERE id = 1]]  
 elseif check == "area6" then  
 db:exec[[UPDATE tb\_prize SET prize = "90" WHERE id = 1]]  
 elseif check == "area7" then  
 db:exec[[UPDATE tb\_prize SET prize = "50" WHERE id = 1]]  
 elseif check == "area8" then  
 db:exec[[UPDATE tb\_prize SET prize = "100" WHERE id = 1]]  
 elseif check == "area9" then  
 db:exec[[UPDATE tb\_prize SET prize = "10" WHERE id = 1]]  
 elseif check == "area10" then  
 db:exec[[UPDATE tb\_prize SET prize = "60" WHERE id = 1]]  
 end   
 director:changeScene("prize")  
 print ("successful")   
end  
  
local function gameResult ()  
 print (check)  
 checkArrow ()  
 print (globalScore)  
 if globalScore == check then  
 for i = 1, 4 do  
 if stopping[i] == 1 then  
 speed[i] = speed[i] - brakes  
 end  
 if speed[i] \<= 0 then  
 speed[i] = 0   
 end  
 if speed[i] == 0 then  
 speed[i] = 0  
 Runtime:removeEventListener( "enterFrame", gameLoop )  
 completed ()  
 end  
 end  
 end  
end  
  
---------------------------------------------------------------------------------------------  
---------------------------------------Game Looping------------------------------------------  
---------------------------------------------------------------------------------------------  
local function applyBrakes(event)  
 for i = 1, 4 do  
 if stopping[i] == 1 then  
 speed[i] = speed[i] - brakes  
 end  
 if speed[i] \<= 4 then  
 speed[i] = 4  
 end  
 if speed[i] == 4 then  
 Runtime:removeEventListener( "enterFrame", checkArea )  
 gameResult ()  
 end  
 end  
end  
  
local function gameLoop()  
 for i = 1, 4 do  
 if arrow.rotation \>= 360 then  
 arrow.rotation = arrow.rotation - (360) + speed[i]  
 else  
 arrow.rotation = math.floor (arrow.rotation + speed[i])  
 end  
 break  
 end  
end  
  
---------------------------------------------------------------------------------------------  
-----------------------------------------Button----------------------------------------------  
---------------------------------------------------------------------------------------------  
local buttonStart = ui.newButton {  
 default = "Images/button.png",  
 over = "Images/button.png",  
 id = "Start",  
 text = "Start",  
 font = native.systemFontBold,  
 textColor = {0,0,0, 255},  
 size = 20,  
 align = "center"  
 }  
 buttonStart.x = W   
 buttonStart.y = H + 150  
  
local buttonStop = ui.newButton {  
 default = "Images/button.png",  
 over = "Images/button.png",  
 id = "Stop",  
 text = "Stop",  
 font = native.systemFontBold,  
 textColor = {0,0,0, 255},  
 size = 20,  
 align = "center"  
 }  
 buttonStop.x = W   
 buttonStop.y = H + 150  
 buttonStop.isVisible = false  
  
---------------------------------------------------------------------------------------------  
--------------------------------------Button Funtion-----------------------------------------  
---------------------------------------------------------------------------------------------  
  
local function stopGame (event)  
 if event.phase == "ended" then  
 Runtime:addEventListener( "enterFrame", applyBrakes )  
 Runtime:addEventListener( "enterFrame", checkArea )  
 buttonStop.isVisible = false  
 for i = 1, 3, 1 do  
 if stopping[i] == 0 then  
 stopping[i] = 1  
 stopping[i+1] = 1  
 break  
 end  
 end  
 end  
end  
  
local function startGame (event)  
 if event.phase == "ended" then  
 buttonStart.isVisible = false  
 buttonStop.isVisible = true  
 Runtime:addEventListener( "enterFrame", gameLoop )  
 end  
end  
  
buttonStart:addEventListener ("touch", startGame)  
buttonStop:addEventListener ("touch", stopGame)  
  
---------------------------------------------------------------------------------------------  
------------------------------------------Group----------------------------------------------  
---------------------------------------------------------------------------------------------  
local function initVars ()   
localGroup:insert(background)  
localGroup:insert(board)  
localGroup:insert(arrow)  
localGroup:insert(buttonStart)  
localGroup:insert(buttonStop)  
end  
  
---------------------------------------------------------------------------------------------  
------------------------------- Clean function ----------------------------------------------  
---------------------------------------------------------------------------------------------  
function clean ( event )  
 print("cleaned")  
end  
---------------------------------------------------------------------------------------------  
-------------------------------- New function -----------------------------------------------  
---------------------------------------------------------------------------------------------  
function new()  
 initVars()   
 return localGroup  
end  

i got error everytime i change the scene…the looping is not stopping at all…what should i do? can someone check it for me? [import]uid: 33180 topic_id: 8165 reply_id: 29447[/import]