Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Highlighted

Community Beginner
,

Apr 10, 2019

Copy link to clipboard

Copied

import flash.events.MouseEvent;

stop();

var x:int=0;

var y:int=0;

addEventListener(MouseEvent.CLICK,startMove);

function startMove(m:MouseEvent){

x=int(Math.random()*10-5);

y=int(Math.random()*10-5);

removeEventListener(MouseEvent.CLICK,startMove);

addEventListener(MouseEvent.CLICK,stopMove);

addEventListener(Event.ENTER_FRAME,moveMe);

}

dx=int(Math.random()*10-5);

What does the 10 and the 5 represent? How do I make it a minimum of one number and a maximum of another?

Adobe Community Professional

Hi.

*Math.random()* returns a number from 0 to 1. So:

- If *Math.random()* returns 0, than 0 * 10 - 5 = -5;

- If *Math.random()* returns 1, than 1 * 10 - 5 = 5.

This means you're gonna get a floating-point number in a range from -5 to 5. -5 being the minimum value and 5 the maximum value.

As the result is being rounded using the *int* class, you get the closest integer that is less than or equal to the specified number or expression. With this being said, the results are:

-4, -3, -2, -1, 0, 1, 2, 3, 4

If you want to include -5 and 5, use *Math.round()* instead of *int()*.

I hope this makes sense.

Regards,

JC

Community Guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Community Beginner
,

Apr 10, 2019

Copy link to clipboard

Copied

import flash.events.MouseEvent;

stop();

var x:int=0;

var y:int=0;

addEventListener(MouseEvent.CLICK,startMove);

function startMove(m:MouseEvent){

x=int(Math.random()*10-5);

y=int(Math.random()*10-5);

removeEventListener(MouseEvent.CLICK,startMove);

addEventListener(MouseEvent.CLICK,stopMove);

addEventListener(Event.ENTER_FRAME,moveMe);

}

dx=int(Math.random()*10-5);

What does the 10 and the 5 represent? How do I make it a minimum of one number and a maximum of another?

Adobe Community Professional

Hi.

*Math.random()* returns a number from 0 to 1. So:

- If *Math.random()* returns 0, than 0 * 10 - 5 = -5;

- If *Math.random()* returns 1, than 1 * 10 - 5 = 5.

This means you're gonna get a floating-point number in a range from -5 to 5. -5 being the minimum value and 5 the maximum value.

As the result is being rounded using the *int* class, you get the closest integer that is less than or equal to the specified number or expression. With this being said, the results are:

-4, -3, -2, -1, 0, 1, 2, 3, 4

If you want to include -5 and 5, use *Math.round()* instead of *int()*.

I hope this makes sense.

Regards,

JC

Community Guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

jamesp67108574

Apr 10, 2019
0
/t5/animate/i-don-t-understand-this-code-example-with-math-random/td-p/10391844
1
Reply
1

Correct answer by
JoãoCésar
LATEST

Adobe Community Professional
,

Apr 10, 2019

Copy link to clipboard

Copied

Hi.

*Math.random()* returns a number from 0 to 1. So:

- If *Math.random()* returns 0, than 0 * 10 - 5 = -5;

- If *Math.random()* returns 1, than 1 * 10 - 5 = 5.

This means you're gonna get a floating-point number in a range from -5 to 5. -5 being the minimum value and 5 the maximum value.

As the result is being rounded using the *int* class, you get the closest integer that is less than or equal to the specified number or expression. With this being said, the results are:

-4, -3, -2, -1, 0, 1, 2, 3, 4

If you want to include -5 and 5, use *Math.round()* instead of *int()*.

I hope this makes sense.

Regards,

JC

__________________________________________

HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Community Guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Reply

Loading...

JoãoCésar

Apr 10, 2019
0
/t5/animate/i-don-t-understand-this-code-example-with-math-random/m-p/10391845#M191924
Using the Community
Experience League
Terms of Use
Privacy Policy
Cookie preferences
AdChoices
Language:

- Deutsch
- English
- Español
- Français
- 日本語コミュニティ
- Português

Copyright © 2020 Adobe. All rights reserved.