Random select

Discussion in 'Javascript' started by A2, May 28, 2007.

  1. A2

    A2 Guest

    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???
     
    A2, May 28, 2007
    #1
    1. Advertising

  2. A2

    Evertjan. Guest

    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.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 28, 2007
    #2
    1. Advertising

  3. A2

    Evertjan. Guest

    Randy Webb wrote on 29 mei 2007 in comp.lang.javascript:

    > Evertjan. said the following on 5/28/2007 5:30 PM:
    >> 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.
    >>

    > 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.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 29, 2007
    #3
  4. On May 28, 3:02 pm, A2 <> wrote:
    > 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())]
    }
     
    scripts.contact, May 29, 2007
    #4
  5. A2

    Lee Guest

    scripts.contact said:
    >
    >On May 28, 3:02 pm, A2 <> wrote:
    >> 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


    --
     
    Lee, May 29, 2007
    #5
  6. In comp.lang.javascript message <
    oglegroups.com>, Tue, 29 May 2007 05:40:42, scripts.contact
    <> posted:
    >On May 28, 3:02 pm, A2 <> wrote:
    >> 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.

    --
    (c) John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Check boilerplate spelling -- error is a public sign of incompetence.
    Never fully trust an article from a poster who gives no full real name.
     
    Dr J R Stockton, May 29, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    470
    mikeb
    Jun 24, 2004
  2. globalrev
    Replies:
    4
    Views:
    784
    Gabriel Genellina
    Apr 20, 2008
  3. Pranjal Jain
    Replies:
    3
    Views:
    173
    saras
    Apr 10, 2008
  4. palmiere
    Replies:
    1
    Views:
    434
    Erwin Moller
    Feb 9, 2004
  5. VK
    Replies:
    15
    Views:
    1,237
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page