Be a Supporter!

random number within range AS help

  • 254 Views
  • 9 Replies
New Topic Respond to this Topic
JorNcar
JorNcar
  • Member since: Apr. 26, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
random number within range AS help 2009-12-21 02:08:37 Reply

hey everyone,
im back making another movie after a very long time but it seems i've run into a small problem.

I've forgotten all the action script code I had once known, and now need to figure out how to make a random number within certain limits. lets say 1 and 5. I'm using flash 8 so I guess it would have to be done in as2.

anyone know how to make a variable (randomnumber) = an actual random number?

I would appreciate the help,
thanks.


Revolution Now

BBS Signature
Montycarlo
Montycarlo
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to random number within range AS help 2009-12-21 02:12:55 Reply

random(limit) // 0 ~ < limit

Range:
var min = 1;
var max = 5;
var myNum:Number = random(max-min)+min

Although practicality beats purity.
Errors should never pass silently.
In the face of ambiguity, refuse the temptation to guess.

JorNcar
JorNcar
  • Member since: Apr. 26, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to random number within range AS help 2009-12-21 02:22:56 Reply

what would i put so when the movie loads, it makes the variable, "juicy" become a random number between 1 and 5?


Revolution Now

BBS Signature
Montycarlo
Montycarlo
  • Member since: Mar. 14, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to random number within range AS help 2009-12-21 02:27:44 Reply

Come on, it's not that hard. The code was self-explanitory.

random(limit) // 0 ~ < limit

var min = 1; // <- MINIMUM
var max = 5; // <- MAXIMUM
var juicy:Number = random(max-min)+min // <- Evaluates into random value.

Although practicality beats purity.
Errors should never pass silently.
In the face of ambiguity, refuse the temptation to guess.

JorNcar
JorNcar
  • Member since: Apr. 26, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to random number within range AS help 2009-12-21 02:30:48 Reply

thank you, much appreciated


Revolution Now

BBS Signature
JorNcar
JorNcar
  • Member since: Apr. 26, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to random number within range AS help 2009-12-21 03:03:07 Reply

so the code you gave me works. but why won't this work. I put it on the next frame. I think what it does is makes juicy first equal 1, then equal 2. so it goes to frame 9 every time... any ideas?

if (juicy =1) {gotoAndPlay(16);

}
if (juicy =2) {gotoAndPlay(9);

}


Revolution Now

BBS Signature
henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to random number within range AS help 2009-12-21 04:18:28 Reply

Exactly, it makes it equal, also know as assigning the value to the variable. You are using the wrong operator, this should have been obvious if you where using the debugger to step the code. Not to mention that compilers often can be told to warn you about assignments in expressions where a condition is expected.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

HonterGames
HonterGames
  • Member since: Jun. 18, 2009
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to random number within range AS help 2009-12-21 05:37:41 Reply

if (juicy ==1) {
gotoAndPlay(16);
}
if (juicy ==2) {
gotoAndPlay(9);
}

BBS Signature
TogaGames
TogaGames
  • Member since: May. 10, 2008
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to random number within range AS help 2009-12-21 07:10:53 Reply

Here's what I use:

function randRange(min:Number, max:Number):Number {
	var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
	return randomNum;
}
var randX:Number = randRange(10, 470); // returns 211
Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to random number within range AS help 2009-12-21 10:33:27 Reply

In terms that are a bit easier to understand, you used the wrong sign. (Called operator)

One equal sign "=" is the 'assignment' operator, and it assigns what's on the right side of it to what's on the left side of it. Example;

variable = 10; Changes variable to 10.

Two equal signs "==" is the 'evaluation' operator and it's used to compare what's on the right side to what's on the left side.

if(variable == 10)


Perpetually looking for time to return to the arts.

BBS Signature