How to make composer to choose values with large difference when called randomly by using math.random?

Hello everybody!

I have a code where I call values randomly by using math.random command.The composer giving values randomly but with the least difference which I don’t want.I want composer to give values with larger difference.How can I achieve that?

Here’s the code

display.setStatusBar( display.HiddenStatusBar ) local Timer local loop local a = 3 local ao = 0.5 local function aa() a = a + 1 print(a) end Timer = timer.performWithDelay ( 10000, aa , 0) local myRectangle = display.newRect(140, -120, 30, 180 ) myRectangle.strokeWidth = 3 myRectangle:setFillColor( ao) myRectangle:setStrokeColor( 1, 0, 0 ) local myRectangle1 = display.newRect(70, -120, 30, 180 ) myRectangle1.strokeWidth = 3 myRectangle1:setFillColor( ao ) myRectangle1:setStrokeColor( 1, 0, 0 ) local myRectangle2 = display.newRect(210, -120, 30, 180 ) myRectangle2.strokeWidth = 3 myRectangle2:setFillColor( ao ) myRectangle2:setStrokeColor( 1, 0, 0 ) local myRectangle3 = display.newRect(280, -120, 30, 180 ) myRectangle3.strokeWidth = 3 myRectangle3:setFillColor( ao ) myRectangle3:setStrokeColor( 1, 0, 0 ) function moveBars(self, event) if self.y \> 670 then ao = math.random(0.6, 1) self.y = math.random( -400, -150) print ( self.y) else self.y = self.y + a end end myRectangle.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle) myRectangle1.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle1) myRectangle2.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle2) myRectangle3.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle3)  

Here’s the corona simulator output values which were printed.

18:30:16.581 Copyright (C) 2009-2016 C o r o n a L a b s I n c . 18:30:16.581 Version: 3.0.0 18:30:16.581 Build: 2016.2906 18:30:16.591 Platform: SM-G900S / x86 / 6.1 / Intel(R) HD Graphics 2500 / 4.0.0 - Build 10.18.10.3431 / 2016.2906 / en\_US | US | en\_US | en 18:30:16.591 Loading project from: C:\Users\Prashant\Documents\Corona Projects\parallax 18:30:16.591 Project sandbox folder: C:\Users\Prashant\AppData\Local\Corona Labs\Corona Simulator\Sandbox\parallax-9EE48529A19BCF5EBA9048F904C9B20D\Documents 18:30:24.692 -239 --- random value of rectangle 1 18:30:24.692 -215 ---- random value of rectangle 2 18:30:24.692 -186 -----random value of rectangle 3 18:30:24.692 -229 ------random value of rectangle 4 18:30:26.632 4 18:30:31.782 -245------random value of rectangle 1 18:30:31.992 -295----random value of rectangle 2 18:30:32.112 -200-----random value of rectangle 3 18:30:32.172 -285---random value of rectangle 4 18:30:36.673 5 18:30:38.543 -161---random value of rectangle 1 18:30:38.543 -248---random value of rectangle 2 18:30:39.063 -244---random value of rectangle 3 18:30:39.133 -253---random value of rectangle 4

As you can see the difference between the values of rectangle1 is not large.(values are -239, -245,-161).I have coded in such a way that the composer has to give any values between -400 & -150.

But I want to make composer to choose random values with some difference.How can I do that? Or have you any other idea? If so, please share.

.

You are asking it to generate:

self.y = math.random( -400, -150)

a number between -400 and -150. And it’s doing just that. In you test run above, you didn’t get anything less than around -300 but you got values close to -150. The problem is your sample of random numbers is pretty small. You generated 12 values out of a range of 250. Random numbers are not going to appear as an even distribution with a sample that small.

Now what you can do is make your range smaller and then multiply by some number to scale the range back. For instance, generate -225 to -150 then multiply time 2. This will give you a more narrow spread of numbers giving each random number a chance to get called but scaling it to make it fit your positioning.

Rob

Yeah I tried doing it previously.And it had solved problem somewhat but not completely.Why I wanted composer to get values with larger difference is, because I wanted to avoid overlapping of rectangles which are placed at random position of their Y in function " moveBars" and then scrolled down. Also I think detecting collision between these rectangles won’t work because once the rectangle has been removed when it collides with other rectangle/s then there will be nothing to scroll again.

So I want help to avoid overlapping of rectangles when they are scrolled and positioned at random of Y. 

But if you have any other idea by which I can implement to perform this operation please tell me.

Try 

local differenceBetweenTwoNumbers = 50 local lastNumber = -400 local nextNumber local rand, abs = math.random, math.abs ... repeat      nextNumber = rand( -400, -150 ) until ( abs( lastNumber - nextNumber ) \> differenceBetweenTwoNumbers )   lastNumber = nextNumber self.y = lastNumber

It is not  perfect but maybe it  will be helpful.

 

Thanks.This method works partially but not completely.Also I have tried other methods, did some math but nothing was helpful.

You are asking it to generate:

self.y = math.random( -400, -150)

a number between -400 and -150. And it’s doing just that. In you test run above, you didn’t get anything less than around -300 but you got values close to -150. The problem is your sample of random numbers is pretty small. You generated 12 values out of a range of 250. Random numbers are not going to appear as an even distribution with a sample that small.

Now what you can do is make your range smaller and then multiply by some number to scale the range back. For instance, generate -225 to -150 then multiply time 2. This will give you a more narrow spread of numbers giving each random number a chance to get called but scaling it to make it fit your positioning.

Rob

Yeah I tried doing it previously.And it had solved problem somewhat but not completely.Why I wanted composer to get values with larger difference is, because I wanted to avoid overlapping of rectangles which are placed at random position of their Y in function " moveBars" and then scrolled down. Also I think detecting collision between these rectangles won’t work because once the rectangle has been removed when it collides with other rectangle/s then there will be nothing to scroll again.

So I want help to avoid overlapping of rectangles when they are scrolled and positioned at random of Y. 

But if you have any other idea by which I can implement to perform this operation please tell me.

Try 

local differenceBetweenTwoNumbers = 50 local lastNumber = -400 local nextNumber local rand, abs = math.random, math.abs ... repeat      nextNumber = rand( -400, -150 ) until ( abs( lastNumber - nextNumber ) \> differenceBetweenTwoNumbers )   lastNumber = nextNumber self.y = lastNumber

It is not  perfect but maybe it  will be helpful.

 

Thanks.This method works partially but not completely.Also I have tried other methods, did some math but nothing was helpful.