Random number generator

I have searched the forum, and have not found the answer to this question:

I need to generate a 6 digit random number from Flash MX, then pass that number as a variable to an .asp page. I know how to pass the variable, but I’m not sure how to generate the number, then set it as a variable…

Thanks in advance,

Rev

random(999999)

How about this rev…

[AS]//create new array to store numbers
var storeArray = [];
//create new string variable
var myString;
//create for loop
for (var i = 0; i<6; i++) {
//in position of i in the array
//store a random number from 0 to 9
storeArray* = random(10);
//add that number to the end of the string
myString += String(storeArray*);
}
//trace the final string of numbers
trace(myString);[/AS]

Is that what you are looking for.

Fluid: That always has a chance of returning a number less than 6 digits.

oh and…

[AS]myString += String(storeArray*);[/AS]

I used String(storeArray*) for this reason:

The array returns a number, so if I used += storeArray*, then the final string would return the value of all those numbers added together.

But if I convert the number to a string, it adds the string version of the number to the end instead of literally adding to the previous number value.

If I am reading this correctly, it would create a variable named MyString and assign it a random 6 digit number…

If this is true, then I understand…

If this isn’t true, I am even more confused (not a surprise)…

thanks

Rev

Yep, that is 100% correct Rev.

wahoooooooo!

I don’t feel so dumb now…

Thanks again lost… way too cool…

Rev

No problem man.

LOL!!! I am definitely not in a coders state of mind today. I don’t know what it is, but all day I have just been sucking with code.

Anywho, I took a relook at this code here, and realized I am storing it in an array for NO reason whatsoever.

So, after removing the array, and moving the random(10) to the myString += I got it to work exactly the same…

[AS]var myString;
for (var i = 0; i<6; i++) {
myString += String(random(10));
}
trace(myString);[/AS]

I was wondering why, but I wasn’t gonna ask…

:beam:

thanks lost, that is very efficient…

Rev

why not simply use

randomVariable = Math.round(Math.random()*1000000)

am i missing something? :slight_smile:

I think Im missing something here as well… we all know that math.round(Math.random) by itself does not produce random numbers that cover the whole spectrum of your range…

but, perhaps Im missing something too…

Because Math.round(Math.random()*1000000) always has a chance of returning less than 6 digit numbers.

And as Rev stated, he needed a 6 digit random number.

So I did what I did because no matter what it will produce a 6 digit random number.

ahmed: your code sometimes returns a 3, 4, or 5 digit number instead of a six. 4 out of 10 tries returned a number that was not 6 digits. Lost’s way ensures that it will be 6 digits long, every time.

I’ll post the formula I like to use tomorrow, and it will always return a number within the correct range including the minimum and maximum number… if I think hard enough, I could just whip it out (the formula)… but Im not gonna… gonna go beddie bye bye. Oh and oooonee mre thhba.a…***;dddd …kjsf…
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Man, I really am not in the right state of mind for coding tonight.

I feel real stupid now :-\

[AS]ranNum = Math.round(Math.random()*(999999-111111))+111111;
trace(ranNum);[/AS]

Works like a charm :hangover:

even cooler…

thanks all ya’ll…

that was one of the last hurdles I had to “finish” this thing…

Rev

Sorry for running you in circles.

I think I will work on actual design of my site tonight instead of coding. I don’t want to screw up the progress I have thus far by being stupid in my coding :stuck_out_tongue:

Glad everything is fixed now :slight_smile:

No fair, you’re getting help with your part. :stuck_out_tongue:

Maybe I should just ask easier questions. :smirk:

*Originally posted by reverendflash *
…that was one of the last hurdles I had to “finish” this thing…

knowledge comes to those who wait…

:trout:

Rev