17 Replies Latest reply on Jan 13, 2007 12:22 PM by kglad

# random odd number with in a range

as title, what i wanted is any odd number being randomly picked from 3-38, and thats the code i have, to let the mc randomly select a frame to go

2nd question for input boxes, how can you add value?

ex:
LE.text = LE +"1" does this work?
• ###### 1. Re: random odd number with in a range
LE.text = LE +"1" does this work?
Try these
LE.text = LE.text + "1";
LE.text += "1";

• ###### 2. Re: random odd number with in a range
thanks for the hint, but im still stuck on getting a odd random number between 3-38
• ###### 3. Re: random odd number with in a range
This should work for you. Remember do not write number types as strings. That means no " " around numbers :) You can delete the trace actions when you see how the code works.

function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
var n:Number = randRange(3, 38);

if (n % 2 == 0) {
trace("'" + n + "' is not an odd number.");
return;
} else {
gotoAndStop(n);
trace(n)
}
• ###### 4. random odd number with in a range
omg thanks a lot .... but when a even number occurs, the mc just stops, i cant use
for (var i = 0; i < 2; i++)
to loop it... how can i loop it then?
• ###### 5. Re: random odd number with in a range
depends........what do you want it to do when an even number occurs?
• ###### 6. Re: random odd number with in a range
for your textfield you probably want to convert your textfield's text property (use the Number() function ) to a number before adding one to it or else you'll be performing string addition

to generate a random odd number between minNum and maxNum you can use:

• ###### 7. Re: random odd number with in a range
i use the number in
gotoandstop
so it jumps to a random frame
• ###### 9. Re: random odd number with in a range
Hmm what do want the even numbers to do that is different to the odd ones?

• ###### 10. Re: random odd number with in a range
he doesn't want even numbers. all the frames he wants to access have odd numbers.
• ###### 11. Re: random odd number with in a range
well I already posted the code for that
• ###### 12. Re: random odd number with in a range
>>well I already posted the code for that

Not really. You posted code that might return an odd number... The OP was
looking for a function to return an odd number between a min and max... not
a function that _might_ return an odd number. Kglad posted the proper
function.

--
Dave -
www.blurredistinction.com

• ###### 13. random odd number with in a range
the code i used:

function randomOddF(3, 38) {
var xmin = Math.floor((3)/2);
var xmax = Math.floor((38-1)/2);
ran = xmin+random(xmax-xmin+1);
return 2*ran+1;
}

if (randomOddF % 2 == 0) {
trace("'" + randomOddF + "' is not an odd number.");
return;
} else {
gotoAndStop(randomOddF);
trace(randomOddF)
}

i got these errors:
"**Error** Symbol=quiz, layer=CMD, frame=2:Line 1: Parameter name expected
function randomOddF(3, 38) {

**Error** Symbol=quiz, layer=CMD, frame=2:Line 6: Unexpected '}' encountered
}"

but if i used

function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
var n:Number = randRange(3, 38);

if (n % 2 == 0) {
trace("'" + n + "' is not an odd number.");
return;
} else {
gotoAndStop(n);
trace(n)
}

it works, but it doesnt loop, so if a even number occurs, it stops...
• ###### 14. Re: random odd number with in a range
You keep the function the way kglad has written it.
Then in your code, you would use:

var p:Number = randomOffF(3,38);
if (a == "18") {
gotoAndPlay(39);
} else {
gotoAndStop(p);
}
• ###### 15. Re: random odd number with in a range
Whoops, meant randomOddF(3,38)...so your entire code should be

var p:Number = randomOddF(3,38);
if (a == "18") {
gotoAndPlay(39);
} else {
gotoAndStop(p);
}
function randomOddF(minNum, maxNum) {
var xmin = Math.floor((minNum)/2);
var xmax = Math.floor((maxNum-1)/2);
ran = xmin+random(xmax-xmin+1);
return 2*ran+1;
}
• ###### 16. random odd number with in a range
ohoh thanks >.< completely forgot about var p:Number

Thanks to all of you guys who helped, thanks a TON, =D
• ###### 17. Re: random odd number with in a range
you're welcome.