Random select

A

A2

How would i make a script that would pick a random variable

ex. I would have and array and the script would pick one of
variables???
 
E

Evertjan.

A2 wrote on 28 mei 2007 in comp.lang.javascript:
How would i make a script that would pick a random variable

ex. I would have and array and the script would pick one of
variables???

You would have to write it.
 
E

Evertjan.

Randy Webb wrote on 29 mei 2007 in comp.lang.javascript:
Evertjan. said the following on 5/28/2007 5:30 PM:
Just make sure you don't search the archives because you might find an
answer to this very question, about 40 times :)

Even so if he wants to make a script, he would have to write it.

Copying from the archives does not amount to "making" in my book.
 
S

scripts.contact

How would i make a script that would pick a random variable

ex. I would have and array and the script would pick one of
variables???

var arr=['a','b','c','d','e','f']

alertRandomItemInArray(arr)

function alertRandomItemInArray(arr){
return arr[Math.round(arr.length*Math.random())]
}
 
L

Lee

scripts.contact said:
How would i make a script that would pick a random variable

ex. I would have and array and the script would pick one of
variables???

var arr=['a','b','c','d','e','f']

alertRandomItemInArray(arr)

function alertRandomItemInArray(arr){
return arr[Math.round(arr.length*Math.random())]
}

When choosing between more than two items, don't use
Math.round(). It will only select the first and the
last item half as often as each of the other items.

To the original poster, before asking questions in a
technical newsgroup, look through the recent articles
to see if any of them point you to the FAQ.

Your question is addressed there:

http://www.jibbering.com/faq/#FAQ4_22


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Tue, 29 May 2007 05:40:42, scripts.contact
How would i make a script that would pick a random variable

ex. I would have and array and the script would pick one of
variables???

var arr=['a','b','c','d','e','f']

alertRandomItemInArray(arr)

function alertRandomItemInArray(arr){
return arr[Math.round(arr.length*Math.random())]
}

Your arr.length = 6. Math.round(6*math.random()) will give 0 (8%), 1-5
(17% each) and 6 (8%). When it gives 6 the return value will be
_undefined_.

Before posting "answer"s, please read the FAQ and test your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top