8 Replies Latest reply on Oct 21, 2006 2:57 PM by rfkrocktk

# Random round?

Hey All,
First off, thanks to kglad for all the sweet help with sound stuff last time. Thanks dude, I owe ya one.

Anyway, I have a question. I'm trying to set a variable (let's call it 'numberizer') to a random number, but I'm trying to set it somewhere between 1 and 10. Is there a limiter for the Math.random(); function? I'm unfamiliar with this as well as the Math.round(X); function, but here is what I have tried so far:

var numberizer:Number = Math.random();
var numberizer:Number = Math.round(10);

Now, I'm assuming that I'm completely wrong on this. If anyone can help me out, that would be amazing. Hopefully it doesn't require an amazing effort to do this.

- TK
• ###### 1. Re: Random round?
if you want to generate a random integer between 1 and 10 you can use:

• ###### 2. Re: Random round?
Hey,

Ok, it's working, almost.

I'm basically trying to tell flash that as soon as it loads, load a random jpg into a movie clip, you know, random background type thing.
I know that the number randomizer is working, I have tested that. However, my pictures aren't randomly loading. It worked fine when I had only two images and had a code that stated:
quote:

if(numberizer > 5){
}
else{
}

That wasn't the full code, but it WAS working.
I have more than two images now, and this is my new code:
quote:

var numberizer:Number = Math.ceil(10*Math.random());
this.onEnterFrame = function() {
if (numberizer==1) {
}
if (numberizer=2) {
}
if (numberizer=3) {
}
if (numberizer=4) {
}
if (numberizer=5) {
}
if (numberizer=6) {
}
};

Seems like a lot of code, and maybe there is an easier way to do this. Either way, if anyone could help me out and help me get this working, I would very much appreciate this. I'm sure it's just some minor flaw in the coding or something.

- TK
• ###### 3. Re: Random round?
You should be using ==, the equality operator, as opposed to =, the assignment operator.

So, in each conditional it is setting numberizer to the given value and then returns the value. As any number besides 0 is equivalent to true, it will call all of those loadMovie's.

Also, if you are going to use that naming pattern, ie img#.jpg, you might as well as use:

var numberizer:Number = Math.ceil(10*Math.random());
• ###### 4. Re: Random round?
Awesome. That's working perfectly.

Can anyone help me out with some xml stuff?

I'll attach my code as well as a link to the zip file containing everythinggggggg.

Basically, I am VERY VERY NEW at XML and flash integration with it. I did a tutorial on this and just added some other functionality such as the random bg image and so forth.
I'm trying to have each 'button' thing in my menu do a different thing: IE have two of the buttons getURL(); and one call a function which will change the background. Plus, the tooltip I am trying to do isn't working, something in my code is messed up. I know this is a ton to ask and it really sucks trying to check for code errors, but would anyone be willing to help me with this?
I'm a visual person and if you are too, just download the zip here:
http://asilverknightsfire.com/homepage.zip

All of the code for XML and basic set up stuff is found in frame 1.
All of the random bg image stuff is inside the imgholder movieclip.

Thank you all for taking the time to help me out, I'm sure it'll all be paid back as soon as I learn this and teach others :)

- TK
• ###### 5. Re: Random round?
.
• ###### 6. Re: Random round?
Alright! Awesome!

Now, how would I call the random background funtion? And do I define that function in flash or XML?
• ###### 7. Re: Random round?
In Flash, on Frame 1, with the rest of your code, you would put:

function randomF(){
var numberizer:Number = Math.ceil(6*Math.random());
}
• ###### 8. Random round?
I'm working on it but I'm supposing I have to put some of my code on the new frame one.

stop();
var xml:XML = new XML();

this.onEnterFrame = function() {
var amount:Number = this.getBytesLoaded() / this.getBytesTotal() *100;
var amount2:Number = xml.getBytesLoaded() / xml.getBytesTotal() * 100;