Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    randomly grabbing *different* elements from an array...

    Hello everyone, l have a quick question that l'm sure one of you vets can help me out with.....

    I've got an array that l want to randomly grab 3 values out of. However l don't want there to be any chance that the array grabs the same value more than once.

    if anyone could help me out, l would greatly appreciate it!

    Samantha Gram

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Array Functionality

    shuffle then take first three
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Or (which is merely the same ideea, but coded in a sigle function:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    var 
    a=[12,3,5,78,7,10,32,41,100,13];
    var 
    nr=3;//number of elements in new radom array
    var r;
    function 
    ran(){
    = new Array();
        for(var 
    i=0;i<nr;i++){
        
    r[i] = a[Math.floor(Math.random()*a.length)];
        var 
    comp r[i];
            for(var 
    j=0;j<r.length-1;j++){
                if(
    comp==r[j]){i--;break}
            }
        }
    }
    onload=function(){ran();alert(r)}
    </script> 
    </head>
    <body>

    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,456
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Shorter still:

    Code:
    var a=[12,3,5,78,7,10,32,41,100,13];
    var nr=3;
    function randOrd(){return (Math.round(Math.random())-0.5); }
    function ran(){
    a.sort(randOrd);
    return a.splice(0,nr);
    }
    onload=function(){alert(ran())}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •