Help with Random Number Generator

Discussion in 'JavaScript' started by SFNUM8, Feb 5, 2011.

  1. #1
    I need a simple Javascript RNG that will pick the numbers 1-5 and write them to screen. But I need it to write all 5 numbers randomly so if a number has already been chosen it would skip that number and loop again until all 5 numbers have been picked.
    Thanks
     
    SFNUM8, Feb 5, 2011 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hogan_h, Feb 5, 2011 IP
  3. SFNUM8

    SFNUM8 Active Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #3
    How would I get this function to print the numbers in the alert box with spaces instead of commas and also how would I get it to print vertical instead of horizontal?

    function getaQuote()
    {
    var myarray=[0, 1, 2, 3, 4, 5]
    myarray.sort(function() {return 0.5 - Math.random()})


    alert("Your Numbers: " + myarray)

    }
     
    SFNUM8, Feb 5, 2011 IP
  4. thewantedhost

    thewantedhost Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Shouldn't an array be:
    var myarray = array("1","2","3","4","5");
    I'm not sure, maybe your way works too.

    var one = myarray[0];
    var two = myarray[1];
    var three = myarray[2];
    var four = myarray[3];
    var five = myarray[4];

    document.write(one+"<br />"+two+"<br />"+three+"<br />"+four+"<br />"+five);

    My JavaScript - not so good.
    That should work.
     
    thewantedhost, Feb 5, 2011 IP
  5. SFNUM8

    SFNUM8 Active Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Your script is missing the sort function, my script works as it is, but I can't figure out the formatting of the output.
     
    SFNUM8, Feb 5, 2011 IP