strange randomness

For questions about using Classic.

Post » Sat Jun 28, 2008 3:26 am

So i try to create this small project were you have to escape falling ball.
no big deal, i use a, what it seem easy event but i got strange result.
every 1000 milisec =>System, Create Object "Sprite" on layer1 at random(0,256,0)

When i run the Cap it goes fine, no error , it spawned regularly my object but the random isn't there.
First object is spawned a 0,0 other is like 128,0 ( kinda halfway of my screen) after all other object are spawned at 0,0.
i what to have them spawned irregularly a bite like rain. Any idea what I've done wrong???
B
2
S
1
G
4
Posts: 114
Reputation: 1,528

Post » Sat Jun 28, 2008 3:48 am

OKay i'm sorry to have created this thread. i found the solution by myself :D yes i'm proud even if it was a stupid reading mistake. When using random number ins MAXSCRIPT ( the only other language i know) you say Rand:(0,128) Random from x to y so when i say the float example i tought hey! its the same thing.. WRONG!!!
But that bring me a question. How to get random number between X and Y when X is not Zero??
like i dunno 100 and 666?
B
2
S
1
G
4
Posts: 114
Reputation: 1,528

Post » Sat Jun 28, 2008 6:31 am

Without knowing the Construct syntax: (Random number between 0 and 566) + 100
B
2
S
1
G
4
Posts: 60
Reputation: 1,320

Post » Sat Jun 28, 2008 10:33 am

Ah, that's a bug: random(1,2,3) is not valid syntax and should only take one parameter - I'll try and fix that one...
Scirra Founder
B
359
S
214
G
72
Posts: 22,948
Reputation: 178,532

Post » Sat Jun 28, 2008 2:34 pm

Towards math freaks.

(music was my first love, math will be the last)

Lets build the formula for random step by step.

Random(c)

c? Yes i dont use 'x' for now. To make you understand that the number we feed to the random function is not coordinate, or height, or any absolute number.
But a count of numbers.

If we need a number between 5 and 7, the count of numbers between them is 3, namely 5, 6 and 7.
I am aware that i leave out the no "whole numbers" to simplify.

So 'c' stands for a count of numbers.
The count of numbers between 3 and 10 = 8. (3 4 5 6 7 8 9 10)

Or to stay in math. c = The end number - starting number + 1

e = end number
s = starting number
c= e - s + 1

so now we are at
Random(e-s+1)

Need a number between 0 and 1 ?

Random(1 - 0 + 1)
or
random(2)

Need a number between 0 and 7 ?

random(7 - 0 + 1)
or
random(8)

But that dont give us the answer for when we need a number between 5 and 7.

To solve this we random the "count of numbers" and add this to the start number.

or
Random(e-s+1) + s

so again, need a number between 0 and 7 ?

random(7 - 0 + 1) + 0
or
random(8)

but if we need a number between 1 and 7 ? !!

random(7 - 1 + 1) + 1
or
random(7) + 1

need a number between 50 and 60 ?

random(60 - 50 + 1) + 50
or
random (11) + 50

there are 11 numbers between 50 and 60 + the 50 where we want to start of with.

But now there is a last thing to bring into the formula. The steps.
Suppose we need a number between 0 and 10, but in steps of 2.
That are the numbers 0, 2, 4, 6, 8 and 10.

The count of numbers is here 6. (You can point with you mouse to the numbers and count them.)

Allow me to use 'd' to represent the steps (the greec delta), i have "s" in use already.

so again.

c = ((End number - Start number) / the steps) + 1
or
random(c) will be

random( [color=#FF0000:1d49dqjd]([/color:1d49dqjd] [color=#000080:1d49dqjd]([/color:1d49dqjd]e-s[color=#0000BF:1d49dqjd])[/color:1d49dqjd] / d [color=#FF0000:1d49dqjd])[/color:1d49dqjd] + 1)

And just like when we want the result to start counting at a certain number.
We need to translate the count of numbers back to absolute numbers

or

random( ( ( ( (e-s) / d ) + 1 ) ) * d ) + s

need a number between 0 and 10 in steps of 2 ?

e = 10
s = 0
d = 2

random ( ( ( ( (e-s) / d ) + 1 ) ) * d ) + s

random ( ( ( ( (10-0) / 2 ) + 1 ) ) * 2 ) + 0
random ( ( ( ( ( 10 ) / 2 ) + 1 ) ) * 2 ) + 0
random ( ( ( ( 5 + 1 ) ) * 2 )
random ( 6 ) * 2
random (6) * 2

need a random number between 10 and 630 in steps of 10 ?

640 could be width of the screen.
We stay 10 pixels from the left edge. And 10 pixels from the right edge.
So we need a number between 10 and 630 in steps of 10

random ( ( ( ( (e-s) / d ) + 1 ) ) * d ) + s
random ( ( ( ( (630-10) / 10 ) + 1 ) ) * 10 ) + 10

or
random((63) * 10) + 10


Hope this helped some out.
B
3
S
2
G
4
Posts: 322
Reputation: 2,119

Post » Sat Jun 28, 2008 2:45 pm

wow. so much meat.. i never expect that much info. Thank you Sir instance
B
2
S
1
G
4
Posts: 114
Reputation: 1,528

Post » Sat Jun 28, 2008 3:59 pm

Towards math haters.

(i am one of those)

Just place Instances of an Object outside screen to represent the "possible places".
Lets say: I want a random number between 0 and 10 step 2.

That are the numbers 0, 2, 4 ,6 , 8 and 10.
IN this .cap i place on object with its X on the possible numbers.

Placing those using the "array past" only takes like 4 seconds of your time.

download/file.php?id=62

Now just randomly pick one of the objects, and use its X for the random number you need.
And ! this is truly random.

Or take this . cap

download/file.php?id=63

Where i used the "array past" to place objects outside screen, to present possible places on the screen by a step of 10.

Just pick a random object for the X, a random for the Y, and feed this to an object that you want to appear on the screen by a 10 * 10 grid.

Or take this .cap

download/file.php?id=64

We need to create objects on random places on the screen. But grid related and on empty places.
Just use the "array paste" to create the possible places, marked by an object.
Pick a random of one of those objects. Create the object on that point.
And destroy the place as possible place, by destroying the object marking the place.

Math is a big help, but its not every thing. : )
B
3
S
2
G
4
Posts: 322
Reputation: 2,119


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests