|
List Info
Thread: Random number - this the best?
|
|
| Random number - this the best? |
  United States |
2007-07-24 15:17:22 |
I'm writing an app that is supposed to draw stars to three
sections of
the screen randomly. I have this function, combined with a
switch
statement. I've done a bit of searching, but is this really
the best
way to get an equal 1/3 chance of execution?
function randRange(min:Number, max:Number) {
var randomNum:Number = Math.floor(Math.random() * (max -
min + 1)) +
min;
return randomNum;
}
var sect:Number = Math.round(randRange(0, 2));
var xTo:Number;
var yTo:Number;
switch(sect){
case 0:
xTo = randRange(40, 225);
yTo = randRange(40, Stage.height -40);
break;
case 1:
xTo = randRange(225, 575);
yTo = randRange(40, 175);
break;
case 2 :
xTo = randRange(575, Stage.width- 40);
yTo = randRange(40, Stage.height - 40);
break;
}
Thoughts?
Daniel Holth
I.S. Programmer
x5217 || J401
This e-mail and its attachments are intended only for the
use of the addressee(s) and may contain privileged,
confidential or proprietary information. If you are not the
intended recipient, or the employee or agent responsible for
delivering the message to the intended recipient, you are
hereby notified that any dissemination, distribution,
displaying, copying, or use of this information is strictly
prohibited. If you have received this communication in
error, please inform the sender immediately and delete and
destroy any record of this message. Thank you.
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| Re: Random number - this the best? |

|
2007-07-24 15:53:38 |
Hi Daniel,
When I'm dealing with low numbers(anything under 10) for
random values I
allways divide 100 by that number and (rounded down of
course) and then
check what the returned number's matching group is (1-33 =
1, 34-66 = 2,
67-99 = 3)
On 7/24/07, Holth, Daniel C. <DCHolth fedins.com> wrote:
>
>
> I'm writing an app that is supposed to draw stars to
three sections of
> the screen randomly. I have this function, combined
with a switch
> statement. I've done a bit of searching, but is this
really the best
> way to get an equal 1/3 chance of execution?
>
> function randRange(min:Number, max:Number) {
> var randomNum:Number = Math.floor(Math.random() *
(max - min + 1)) +
> min;
> return randomNum;
> }
>
>
> var sect:Number = Math.round(randRange(0, 2));
> var xTo:Number;
> var yTo:Number;
> switch(sect){
> case 0:
> xTo = randRange(40, 225);
> yTo = randRange(40, Stage.height -40);
> break;
> case 1:
> xTo = randRange(225, 575);
> yTo = randRange(40, 175);
> break;
> case 2 :
> xTo = randRange(575, Stage.width- 40);
> yTo = randRange(40, Stage.height -
40);
> break;
> }
>
> Thoughts?
>
> Daniel Holth
> I.S. Programmer
> x5217 || J401
>
>
>
> This e-mail and its attachments are intended only for
the use of the
> addressee(s) and may contain privileged, confidential
or proprietary
> information. If you are not the intended recipient, or
the employee or agent
> responsible for delivering the message to the intended
recipient, you are
> hereby notified that any dissemination, distribution,
displaying, copying,
> or use of this information is strictly prohibited. If
you have received this
> communication in error, please inform the sender
immediately and delete and
> destroy any record of this message. Thank you.
> _______________________________________________
> Flashnewbie chattyfig.figleaf.com
> To change your subscription options or search the
archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.c
om
>
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| RE: Random number - this the best? |
  United States |
2007-07-24 15:56:53 |
Here's what I've used for years with no problems:
function rndNum(minVal:Number, maxVal:Number):Number
{
return minVal +
Math.floor(Math.random()*(maxVal+1-minVal));
}
Jason Merrill
Bank of America
GT&O Learning & Leadership Development
eTools & Multimedia Team
>>-----Original Message-----
>>From: flashnewbie-bounces chattyfig.figleaf.com
>>[mailto:flashnewbie-bounces chattyfig.figleaf.com] On
Behalf
>>Of Holth, Daniel C.
>>Sent: Tuesday, July 24, 2007 4:17 PM
>>To: flashnewbie chattyfig.figleaf.com
>>Subject: [Flashnewbie] Random number - this the
best?
>>
>>
>>I'm writing an app that is supposed to draw stars to
three
>>sections of the screen randomly. I have this
function,
>>combined with a switch statement. I've done a bit
of
>>searching, but is this really the best way to get an
equal
>>1/3 chance of execution?
>>
>>function randRange(min:Number, max:Number) {
>> var randomNum:Number = Math.floor(Math.random() *
(max -
>>min + 1)) + min;
>> return randomNum;
>>}
>>
>>
>>var sect:Number = Math.round(randRange(0, 2)); var
>>xTo:Number; var yTo:Number; switch(sect){
>> case 0:
>> xTo = randRange(40, 225);
>> yTo = randRange(40, Stage.height -40);
>> break;
>> case 1:
>> xTo = randRange(225, 575);
>> yTo = randRange(40, 175);
>> break;
>> case 2 :
>> xTo = randRange(575, Stage.width- 40);
>> yTo = randRange(40, Stage.height - 40);
>> break;
>>}
>>
>>Thoughts?
>>
>>Daniel Holth
>>I.S. Programmer
>>x5217 || J401
>>
>>
>>
>>This e-mail and its attachments are intended only
for the use
>>of the addressee(s) and may contain privileged,
confidential
>>or proprietary information. If you are not the
intended
>>recipient, or the employee or agent responsible for
>>delivering the message to the intended recipient,
you are
>>hereby notified that any dissemination,
distribution,
>>displaying, copying, or use of this information is
strictly
>>prohibited. If you have received this communication
in error,
>>please inform the sender immediately and delete and
destroy
>>any record of this message. Thank you.
>>_______________________________________________
>>Flashnewbie chattyfig.figleaf.com
>>To change your subscription options or search the
archive:
>>http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
>>
>>Brought to you by Fig Leaf Software
>>Premier Authorized Adobe Consulting and Training
>>http://www.figleaf.com http://training.figleaf.c
om
>>
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| Re: Random number - this the best? |

|
2007-07-24 16:54:46 |
On 7/24/07, Bob Wohl <bob.wohl gmail.com> wrote:
> Hi Daniel,
>
> When I'm dealing with low numbers(anything under 10)
for random values I
> allways divide 100 by that number and (rounded down of
course) and then
> check what the returned number's matching group is
(1-33 = 1, 34-66 = 2,
> 67-99 = 3)
You can do that with all kinds of distributions, btw.
Something like that:
class Distribution {
var outcomes : Array;
var total : Number;
public function Distribution () {
outcomes = [];
total = 0;
}
public function addOutcome ( result : Object, proportion
: Number ) : Void {
if( !proportion ) proportion = 1; // default value
if( proportion < 0 ) proportion = 0; // must be
positive, ignore
total += proportion;
outcomes.push( { result: result, proportion:
proportion } );
}
public function roll () : Object {
var rand : Number = Math.random() * total;
var count : Number = 0;
var candidate : Object;
for( var i in outcomes ) {
candidate = outcomes[ i ];
count += candidate.proportion;
if( rand < count ) {
return candidate.result;
}
}
}
}
You'd use it like that:
var dice = new Distribution();
dice.addOutcome( "one", 1 );
dice.addOutcome( "two", 1 );
dice.addOutcome( "three", 1 );
dice.addOutcome( "four", 1 );
dice.addOutcome( "five", 1 );
dice.addOutcome( "six", 5 ); // strong bias
trace( dice.roll() );
That would result in a dice that would roll a six every
second throw.
You could also pass functions:
var eat = function () {
trace( "yum, yum, yum..." );
};
var sleep = function () {
trace( "Zzzzzz..." );
};
var foo = new Distribution();
foo.addOutcome( eat, 2 );
foo.addOutcome( sleep, 4 );
var action = foo.roll();
action();
// or simply foo.roll()();
Note that the proportions wouldn't have to add up to
something
special, like percentages. They could be any number, the
higher, the
more likely.
Mark
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| RE: Random number - this the best? |
  United States |
2007-07-25 15:15:15 |
Thanks for all the help folks! A lot of good solutions
here!
Daniel Holth
I.S. Programmer
x5217 || J401
-----Original Message-----
From: flashnewbie-bounces chattyfig.figleaf.com
[mailto:flashnewbie-bounces chattyfig.figleaf.com] On
Behalf Of Mark
Winterhalder
Sent: Tuesday, July 24, 2007 4:55 PM
To: Flashnewbie Mailing List
Subject: Re: [Flashnewbie] Random number - this the best?
On 7/24/07, Bob Wohl <bob.wohl gmail.com> wrote:
> Hi Daniel,
>
> When I'm dealing with low numbers(anything under 10)
for random values
I
> allways divide 100 by that number and (rounded down of
course) and
then
> check what the returned number's matching group is
(1-33 = 1, 34-66 =
2,
> 67-99 = 3)
You can do that with all kinds of distributions, btw.
Something like
that:
class Distribution {
var outcomes : Array;
var total : Number;
public function Distribution () {
outcomes = [];
total = 0;
}
public function addOutcome ( result : Object, proportion
: Number ) :
Void {
if( !proportion ) proportion = 1; // default value
if( proportion < 0 ) proportion = 0; // must be
positive, ignore
total += proportion;
outcomes.push( { result: result, proportion:
proportion } );
}
public function roll () : Object {
var rand : Number = Math.random() * total;
var count : Number = 0;
var candidate : Object;
for( var i in outcomes ) {
candidate = outcomes[ i ];
count += candidate.proportion;
if( rand < count ) {
return candidate.result;
}
}
}
}
You'd use it like that:
var dice = new Distribution();
dice.addOutcome( "one", 1 );
dice.addOutcome( "two", 1 );
dice.addOutcome( "three", 1 );
dice.addOutcome( "four", 1 );
dice.addOutcome( "five", 1 );
dice.addOutcome( "six", 5 ); // strong bias
trace( dice.roll() );
That would result in a dice that would roll a six every
second throw.
You could also pass functions:
var eat = function () {
trace( "yum, yum, yum..." );
};
var sleep = function () {
trace( "Zzzzzz..." );
};
var foo = new Distribution();
foo.addOutcome( eat, 2 );
foo.addOutcome( sleep, 4 );
var action = foo.roll();
action();
// or simply foo.roll()();
Note that the proportions wouldn't have to add up to
something
special, like percentages. They could be any number, the
higher, the
more likely.
Mark
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
This e-mail and its attachments are intended only for the
use of the addressee(s) and may contain privileged,
confidential or proprietary information. If you are not the
intended recipient, or the employee or agent responsible for
delivering the message to the intended recipient, you are
hereby notified that any dissemination, distribution,
displaying, copying, or use of this information is strictly
prohibited. If you have received this communication in
error, please inform the sender immediately and delete and
destroy any record of this message. Thank you.
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| Button Issue |
  United States |
2007-07-25 15:42:17 |
Hello ,
I m creating an interface for a home page.
I want it to present weekly events when you mouse over the
various
days of the week.
While the button works great, when you mouse over the area
where the
" over " state content is displayed,it shows the
button over state
details, even though I am not moused over the actual
button
Any Ideas?
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| Re: Button Issue |
  United States |
2007-07-25 16:26:16 |
Are you using buttons or mc's as buttons?
Marko wrote:
> Hello ,
> I m creating an interface for a home page.
>
> I want it to present weekly events when you mouse over
the various
> days of the week.
> While the button works great, when you mouse over the
area where the "
> over " state content is displayed,it shows the
button over state
> details, even though I am not moused over the actual
button
>
> Any Ideas?
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| Re: Button Issue |
  United States |
2007-07-25 16:32:06 |
I am using Buttons.
On Jul 25, 2007, at 2:26 PM, Steve Rachels wrote:
> Are you using buttons or mc's as buttons?
>
>
> Marko wrote:
>> Hello ,
>> I m creating an interface for a home page.
>>
>> I want it to present weekly events when you mouse
over the various
>> days of the week.
>> While the button works great, when you mouse over
the area where
>> the " over " state content is
displayed,it shows the button over
>> state details, even though I am not moused over
the actual button
>>
>> Any Ideas?
> _______________________________________________
> Flashnewbie chattyfig.figleaf.com
> To change your subscription options or search the
archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.c
om
Marko
marko m2mediadesigns.com
http://www.m2mediadesig
ns.com
760-534-3351
Check out M2 Media Designs Current Specials
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| Re: Button Issue |
  United States |
2007-07-25 16:51:21 |
Sounds like you have the weekly events inside the over state
of the
button (as opposed to having it in a separate mc or text
area). That
would cause the button to 'expand' to fill that area. If
that's what's
happening, the simplest thing to do would be make the weekly
events in a
separate mc that's called when you mouse over the button.
Only thing I can think of.
Marko wrote:
> I am using Buttons.
>
> On Jul 25, 2007, at 2:26 PM, Steve Rachels wrote:
>
>> Are you using buttons or mc's as buttons?
>>
>>
>> Marko wrote:
>>> Hello ,
>>> I m creating an interface for a home page.
>>>
>>> I want it to present weekly events when you
mouse over the various
>>> days of the week.
>>> While the button works great, when you mouse
over the area where the
>>> " over " state content is
displayed,it shows the button over state
>>> details, even though I am not moused over the
actual button
>>>
>>> Any Ideas?
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
| Re: Button Issue |

|
2007-07-25 17:55:44 |
On Jul 25, 2007, at 4:42 PM, Marko wrote:
> I want it to present weekly events when you mouse over
the various
> days of the week.
> While the button works great, when you mouse over the
area where
> the " over " state content is displayed,it
shows the button over
> state details, even though I am not moused over the
actual button
Sounds like your hit state is matching your over state. If
you want
this to all be contained in a single button, you probably
need to
copy your (presumably smaller) up state and paste it into
your hit
state to replace whatever is currently there.
--
Troy
RPSystems, Ltd.
http://www.rpsystems.net
_______________________________________________
Flashnewbie chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashnewb
ie
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.c
om
|
|
|
|