# Adding a little randomness

**URL:** https://forum.kirupa.com/t/adding-a-little-randomness/4962
**Category:** Uncategorized
**Created:** [August 27, 2002, 2:27am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962 "2002-08-27T02:27:36Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Ryall](https://avatars.discourse-cdn.com/v4/letter/r/53a042/32.png) [@Ryall](https://forum.kirupa.com/u/Ryall)
#### Post date: [August 27, 2002, 2:27am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/1 "2002-08-27T02:27:36Z")

</div>

currently I have an animation which continully repeats the letter i moving across the screen. It is reset when the i comes in contact with a MC called reset. Here is the code I am using:  
onClipEvent (enterFrame) {  
if (this.hitTest(\_root.reset)) {  
this.\_x = -410;  
} else {  
speed = .5;  
this.\_x += speed;  
}  
}  
Currently I have a dozen or so MCs at different sizes, alphas and y positions. I would like to modify this code so when they reappear they are randomly placed withing a y\_range, randomly given alpha values within a range (ie between 10% and 35%), and random speed values (ie between .1 and 1.9)… also if possible Id like to include random size (but this is not necessary.  
Please help!!! 🙂 Thanks  
Peace

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 4:03am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/2 "2002-08-27T04:03:36Z")

</div>

That is the power of Math.random().

for your \_y position it would look like

this.\_y = Math.random()\*400; (if the height of your movie is 400)

You can use this for all the properties that require numbers.

For your size problem, that would be \_xscale and \_yscale, to keep these the same, so there isn’t any oblong distortion you can do this…

mySize = Math.random()\*100 //maximum size you want to go  
this.\_xscale = mySize;  
this.\_yscale = mySize;

If it lands on 100, your graphic will be 100 x 100

NOTE: To have non-decimal numbers you will need to do this…

Math.round(Math.random()\*400);

I hope this helps at all.🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 4:14am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/3 "2002-08-27T04:14:57Z")

</div>

thank you! one question…  
if I want to get random alpha values between 0 and 45% I put

this.alpha=Math.random()\*45 right?, but what if I want values between 15% and 45%??? what then???

Peace

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 4:24am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/4 "2002-08-27T04:24:30Z")

</div>

Hmm, good question, I have never done anything like that. Possibly…

if (this.\_alpha \< 15) {  
this.\_alpha = 15;  
}

Maybe that will work:)

By The Way - you wrote it right

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 4:46am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/5 "2002-08-27T04:46:32Z")

</div>

thanks… I’ll let you know what works!!!  
Peace

PS one more question… if I place the random function inside my if else loop wont it be given new values constantly until it hits the reset MC???

basiclly I dont want any values to change until reset, then I want those values to remain until once again hitting reset…hmmm???

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 4:56am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/6 "2002-08-27T04:56:22Z")

</div>

No, it should be given a constant value until hitting the reset clip. After it hits the reset clip it should be given new values.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 4:59am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/7 "2002-08-27T04:59:08Z")

</div>

I put it like this  
onClipEvent (enterFrame) {  
if (this.hitTest(\_root.reset)) {  
this.\_x = -410;  
} else {  
speed = Math.random()\*2;  
this.\_x += speed;  
}  
}  
this makes the MC fluctuate its speed constantly

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 5:07am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/8 "2002-08-27T05:07:40Z")

</div>

Try something like this…

```auto

onClipEvent (load) {
	speed = Math.random()*2;
}
onClipEvent (enterFrame) {
	this._x += speed;
	if (this.hitTest(_root.reset)) {
		this._x = -410;
	}
}

```

I just noticed everything was in a onEnterFrame, if you load the variables in the onLoad, everything should stay constant.

EDIT: This solution doesn’t solve it so that after it hits, the objects get new values. I will see what I can do about that.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 5:08am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/9 "2002-08-27T05:08:41Z")

</div>

ok now I think i have it

onClipEvent (enterFrame) {  
if (this.hitTest(\_root.reset)) {  
this.\_x = -410;  
speed = Math.random()\*2  
} else {  
this.\_x += speed;  
}  
}

but the only way I can make this work is if I start the MC off in contact with the MC reset. I’d like to assign a beginning value that gets dumped and randomized after the first reset? is there some null command I sould use… advice? Thanks  
Peace

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 5:09am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/10 "2002-08-27T05:09:25Z")

</div>

using that will the value change every time it resets?  
nevermind I got it using this code:

onClipEvent (load) {  
speed = Math.random()\*2;  
}  
onClipEvent (enterFrame) {  
this.\_x += speed;  
if (this.hitTest(\_root.reset)) {  
this.\_x = -300;  
speed = Math.random()\*2  
}  
}

now it starts with a random then EVERY time it resets it travels with a new random speed.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 27, 2002, 5:16am UTC](https://forum.kirupa.com/t/adding-a-little-randomness/4962/11 "2002-08-27T05:16:38Z")

</div>

HAHA, I fixed it that way too, I just didn’t get a chance to post beacuse I was talking to someone.
