Random rotation not random

I’m building an app designed to spin a wheel and pick a random number based on the angle of rotation. The problem is the program spins the same exact amount every single time it is run regardless of the speed inputted into torque. 

display.setDefault("background", 1, 1, 1) --function scene:create( event ) --local sceneGroup = self.view physics.start() physics.setGravity(0,0) physics.setDrawMode("normal") --physics.pause() --local wheel = display.newImage(sceneGroup, "arrow.png", true) local wheel = display.newImage("arrow.png") --wheel.x = display.contentWidth - 195 wheel.x = display.contentCenterX wheel.y = display.contentCenterY --wheel.y = display.contentHeight - 268 physics.addBody(wheel, "dynamic",{bounce=0, friction=0.2, radius=1}) wheel.isFixedRotation = false --local nail = display.newCircle(sceneGroup, display.contentCenterX, display.contentCenterY, 4) --physics.addBody(nail, "static",{bounce=0, friction=0.2, radius=4}) --local joint = physics.newJoint("pivot", wheel, nail, display.contentCenterX, display.contentCenterY) --joint.isLimitEnabled = false local options = { --parent = textGroup, text = "0", x = display.contentCenterX, y = display.contentCenterY - 200, width = 228, --required for multi-line and alignment font = native.systemFontBold, fontSize = 19, align = "center" --new alignment parameter } local myText = display.newText( options ) myText:setFillColor( 0, 0, 0 ) function go() math.randomseed( os.time() ) goo = true local speed = math.random( -500, 500 ) wheel.angularDamping = math.random( .6, .8 ) wheel:applyTorque(speed) end go() spinObject = function(event) return true end function printTimeSinceStart( event ) myText.text = "("..math.deg(math.asin(math.sin(wheel.rotation)))..")" if wheel.angularVelocity == 0 and goo == true then print(math.abs(math.deg(math.asin(math.sin(wheel.rotation))))) go() end end Runtime:addEventListener( "enterFrame", printTimeSinceStart ) wheel:addEventListener("tap", spinObject) --end

Have you tried moving your randomSeed() call to your main.lua?

I suggest initializing the seed ONCE only. 

I’ll try that (although the speeds generated by math.random appear to be random in testing) but I have another, probably related problem; the code completely fails after building it to Android as it just spins uncontrollably without stopping. Any help would be appreciated.

If you’re trying to achieve what I think you’re trying to achieve, it’s just a question of the numbers you’re using. Try setting the friction on the wheel higher (say, 0.8). I don’t think that randomising your angular damping is helping, either. Try setting it to 1 - at least for testing. I’d also set your speed/torque variations much lower. Like, -50 to 50.

I used the above numbers and it spins for a couple seconds at a ‘sensible speed’ and stops, then restarts. Not sure if thats what you wanted?

When that does roughly what you want, you can try randomising the values a bit more! :slight_smile:

My goal is to build a wheel that spins at a random speed, for a random amount of time and lands at a random angle. Is there any reason that the wheel, regardless of its starting speed, seems to always land in the same place? After applying your recommendations, I ran 6 tests, each with different hard-coded speeds, and they always had the exact same results each time. Am I misunderstanding what applyTorque() actually does or is that the wrong way to calculate the angle offset from its original position? Thanks for your help in advance. 

Results:

14:50:56.842 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:50:56.842 Version: 3.0.0 14:50:56.842 Build: 2016.2830 14:50:56.842 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:50:56.842 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:50:56.842 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:50:56.852 ----- 14:50:56.852 100 14:51:04.178 56.702623894922 14:51:04.178 ----- 14:51:04.178 100 14:51:11.434 66.678681574678 14:51:11.434 ----- 14:51:11.434 100 14:51:18.670 10.143916408798 14:51:18.670 ----- 14:51:18.670 100 14:51:37.342 14:51:37.342 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:51:37.342 Version: 3.0.0 14:51:37.342 Build: 2016.2830 14:51:37.352 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:51:37.352 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:51:37.352 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:51:37.352 ----- 14:51:37.352 50 14:51:44.675 56.702623894922 14:51:44.675 ----- 14:51:44.675 50 14:51:51.935 66.678681574678 14:51:51.935 ----- 14:51:51.935 50 14:51:59.196 10.143916408798 14:51:59.196 ----- 14:51:59.196 50 14:52:10.846 14:52:10.846 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:52:10.846 Version: 3.0.0 14:52:10.846 Build: 2016.2830 14:52:10.846 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:52:10.846 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:52:10.846 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:52:10.846 ----- 14:52:10.846 25 14:52:18.171 56.702623894922 14:52:18.171 ----- 14:52:18.171 25 14:52:25.436 66.678681574678 14:52:25.436 ----- 14:52:25.436 25 14:52:32.672 10.143916408798 14:52:32.672 ----- 14:52:32.672 25 14:52:48.345 14:52:48.345 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:52:48.345 Version: 3.0.0 14:52:48.345 Build: 2016.2830 14:52:48.355 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:52:48.355 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:52:48.355 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:52:48.355 ----- 14:52:48.355 -25 14:52:55.706 56.702623894922 14:52:55.706 ----- 14:52:55.706 -25 14:53:02.972 66.678681574678 14:53:02.972 ----- 14:53:02.972 -25 14:53:10.198 10.143916408798 14:53:10.198 ----- 14:53:10.198 -25 14:53:22.379 14:53:22.379 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:53:22.379 Version: 3.0.0 14:53:22.379 Build: 2016.2830 14:53:22.379 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:53:22.379 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:53:22.379 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:53:22.379 ----- 14:53:22.379 -50 14:53:29.672 56.702623894922 14:53:29.672 ----- 14:53:29.672 -50 14:53:36.902 66.678681574678 14:53:36.902 ----- 14:53:36.902 -50 14:53:44.184 10.143916408798 14:53:44.184 ----- 14:53:44.184 -50 14:54:00.341 14:54:00.341 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:54:00.341 Version: 3.0.0 14:54:00.341 Build: 2016.2830 14:54:00.341 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:54:00.352 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:54:00.352 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:54:00.352 ----- 14:54:00.352 -100 14:54:07.711 56.702623894922 14:54:07.711 ----- 14:54:07.711 -100 14:54:14.970 66.678681574678 14:54:14.970 ----- 14:54:14.970 -100 14:54:22.197 10.143916408798 14:54:22.197 ----- 14:54:22.197 -100

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- hide the status bar -- include the Corona "composer" module local composer = require "composer" display.setDefault("background", 1, 1, 1) physics.start() physics.setGravity(0,0) physics.setDrawMode("normal") local wheel = display.newImage("arrow.png") wheel.x = display.contentCenterX wheel.y = display.contentCenterY physics.addBody(wheel, "dynamic",{bounce=0, friction=0.8, radius=1}) wheel.isFixedRotation = false local options = { text = "0", x = display.contentCenterX, y = display.contentCenterY - 200, width = 228, font = native.systemFontBold, fontSize = 19, align = "center" } local myText = display.newText( options ) myText:setFillColor( 0, 0, 0 ) math.randomseed( os.time() ) function go() goo = true --local speed = math.random( -50, 50 ) local speed = -100 print("-----") print(speed) wheel.angularDamping = 1 wheel:applyTorque(speed) end go() spinObject = function(event) return true end function printTimeSinceStart( event ) myText.text = "("..math.deg(math.asin(math.sin(wheel.rotation)))..")" if wheel.angularVelocity == 0 and goo == true then print(math.abs(math.deg(math.asin(math.sin(wheel.rotation))))) go() end end Runtime:addEventListener( "enterFrame", printTimeSinceStart ) wheel:addEventListener("tap", spinObject)

Why so you want to use physics at all for this kind of purpose?

The physic engine is not deterministic, so the result is not reliable.

I would suggest to generate the random number up front and then use transition to rotate the wheel to the specific angle (with some extra spins and stuff to make it more realistic)

Have you tried moving your randomSeed() call to your main.lua?

I suggest initializing the seed ONCE only. 

I’ll try that (although the speeds generated by math.random appear to be random in testing) but I have another, probably related problem; the code completely fails after building it to Android as it just spins uncontrollably without stopping. Any help would be appreciated.

If you’re trying to achieve what I think you’re trying to achieve, it’s just a question of the numbers you’re using. Try setting the friction on the wheel higher (say, 0.8). I don’t think that randomising your angular damping is helping, either. Try setting it to 1 - at least for testing. I’d also set your speed/torque variations much lower. Like, -50 to 50.

I used the above numbers and it spins for a couple seconds at a ‘sensible speed’ and stops, then restarts. Not sure if thats what you wanted?

When that does roughly what you want, you can try randomising the values a bit more! :slight_smile:

My goal is to build a wheel that spins at a random speed, for a random amount of time and lands at a random angle. Is there any reason that the wheel, regardless of its starting speed, seems to always land in the same place? After applying your recommendations, I ran 6 tests, each with different hard-coded speeds, and they always had the exact same results each time. Am I misunderstanding what applyTorque() actually does or is that the wrong way to calculate the angle offset from its original position? Thanks for your help in advance. 

Results:

14:50:56.842 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:50:56.842 Version: 3.0.0 14:50:56.842 Build: 2016.2830 14:50:56.842 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:50:56.842 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:50:56.842 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:50:56.852 ----- 14:50:56.852 100 14:51:04.178 56.702623894922 14:51:04.178 ----- 14:51:04.178 100 14:51:11.434 66.678681574678 14:51:11.434 ----- 14:51:11.434 100 14:51:18.670 10.143916408798 14:51:18.670 ----- 14:51:18.670 100 14:51:37.342 14:51:37.342 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:51:37.342 Version: 3.0.0 14:51:37.342 Build: 2016.2830 14:51:37.352 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:51:37.352 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:51:37.352 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:51:37.352 ----- 14:51:37.352 50 14:51:44.675 56.702623894922 14:51:44.675 ----- 14:51:44.675 50 14:51:51.935 66.678681574678 14:51:51.935 ----- 14:51:51.935 50 14:51:59.196 10.143916408798 14:51:59.196 ----- 14:51:59.196 50 14:52:10.846 14:52:10.846 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:52:10.846 Version: 3.0.0 14:52:10.846 Build: 2016.2830 14:52:10.846 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:52:10.846 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:52:10.846 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:52:10.846 ----- 14:52:10.846 25 14:52:18.171 56.702623894922 14:52:18.171 ----- 14:52:18.171 25 14:52:25.436 66.678681574678 14:52:25.436 ----- 14:52:25.436 25 14:52:32.672 10.143916408798 14:52:32.672 ----- 14:52:32.672 25 14:52:48.345 14:52:48.345 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:52:48.345 Version: 3.0.0 14:52:48.345 Build: 2016.2830 14:52:48.355 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:52:48.355 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:52:48.355 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:52:48.355 ----- 14:52:48.355 -25 14:52:55.706 56.702623894922 14:52:55.706 ----- 14:52:55.706 -25 14:53:02.972 66.678681574678 14:53:02.972 ----- 14:53:02.972 -25 14:53:10.198 10.143916408798 14:53:10.198 ----- 14:53:10.198 -25 14:53:22.379 14:53:22.379 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:53:22.379 Version: 3.0.0 14:53:22.379 Build: 2016.2830 14:53:22.379 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:53:22.379 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:53:22.379 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:53:22.379 ----- 14:53:22.379 -50 14:53:29.672 56.702623894922 14:53:29.672 ----- 14:53:29.672 -50 14:53:36.902 66.678681574678 14:53:36.902 ----- 14:53:36.902 -50 14:53:44.184 10.143916408798 14:53:44.184 ----- 14:53:44.184 -50 14:54:00.341 14:54:00.341 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 14:54:00.341 Version: 3.0.0 14:54:00.341 Build: 2016.2830 14:54:00.341 Platform: SM-G900S / x64 / 10.0 / GeForce GTX 980 Ti/PCIe/SSE2 / 4.5.0 NVIDIA 361.43 / 2016.2830 / en\_US | US | en\_US | en 14:54:00.352 Loading project from: C:\Users\Cameron\Documents\Corona Projects\Wheel 14:54:00.352 Project sandbox folder: C:\Users\Cameron\AppData\Local\Corona Labs\Corona Simulator\Sandbox\wheel-6F862B5AF3F264489A136665C4315D7C\Documents 14:54:00.352 ----- 14:54:00.352 -100 14:54:07.711 56.702623894922 14:54:07.711 ----- 14:54:07.711 -100 14:54:14.970 66.678681574678 14:54:14.970 ----- 14:54:14.970 -100 14:54:22.197 10.143916408798 14:54:22.197 ----- 14:54:22.197 -100

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- hide the status bar -- include the Corona "composer" module local composer = require "composer" display.setDefault("background", 1, 1, 1) physics.start() physics.setGravity(0,0) physics.setDrawMode("normal") local wheel = display.newImage("arrow.png") wheel.x = display.contentCenterX wheel.y = display.contentCenterY physics.addBody(wheel, "dynamic",{bounce=0, friction=0.8, radius=1}) wheel.isFixedRotation = false local options = { text = "0", x = display.contentCenterX, y = display.contentCenterY - 200, width = 228, font = native.systemFontBold, fontSize = 19, align = "center" } local myText = display.newText( options ) myText:setFillColor( 0, 0, 0 ) math.randomseed( os.time() ) function go() goo = true --local speed = math.random( -50, 50 ) local speed = -100 print("-----") print(speed) wheel.angularDamping = 1 wheel:applyTorque(speed) end go() spinObject = function(event) return true end function printTimeSinceStart( event ) myText.text = "("..math.deg(math.asin(math.sin(wheel.rotation)))..")" if wheel.angularVelocity == 0 and goo == true then print(math.abs(math.deg(math.asin(math.sin(wheel.rotation))))) go() end end Runtime:addEventListener( "enterFrame", printTimeSinceStart ) wheel:addEventListener("tap", spinObject)

Why so you want to use physics at all for this kind of purpose?

The physic engine is not deterministic, so the result is not reliable.

I would suggest to generate the random number up front and then use transition to rotate the wheel to the specific angle (with some extra spins and stuff to make it more realistic)