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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to do Random pick from array with no repeat...

    My array contains 15 value I want to random pick 5 value:

    Code:
     myArray[0] ="a";
     myArray[1] ="b";
     myArray[2] ="c";
     myArray[3] ="d";
     myArray[4] ="e";
     myArray[5] ="f";
     myArray[6] ="g";
     myArray[7] ="h";
     myArray[8] ="i";
     myArray[9] ="j";
     myArray[10] ="k";
     myArray[11] ="l";
     myArray[12] ="a";
     myArray[13] ="b";
     myArray[14] ="c";


    How to random pick from all elements with no repeat of the same content.
    e.g. if myArray[0] is picked then myArray[12] will not be picked again. (because they have the same value "a")

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by mazdaspring View Post
    My array contains 15 value I want to random pick 5 value:

    Code:
     myArray[0] ="a";
     myArray[1] ="b";
     myArray[2] ="c";
     myArray[3] ="d";
     myArray[4] ="e";
     myArray[5] ="f";
     myArray[6] ="g";
     myArray[7] ="h";
     myArray[8] ="i";
     myArray[9] ="j";
     myArray[10] ="k";
     myArray[11] ="l";
     myArray[12] ="a";
     myArray[13] ="b";
     myArray[14] ="c";


    How to random pick from all elements with no repeat of the same content.
    e.g. if myArray[0] is picked then myArray[12] will not be picked again. (because they have the same value "a")
    If you are able to write the basic javascript, in the loop concantenate the results into a string then check the string for a repeat prior to adding a new character. Let me know if you dont understand and I will code it for you.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    simply shuffle the array and use [].pop() to draw one index at a from the top; you'll never repeat.



    Code:
    //define array:
    myArray=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "a", "b", "c"];
    
     //shuffle array:
    myArray.sort(function(){return Math.round(Math.random());});
    
    
    //show 5 picks from shuffled array:
    alert( [
     myArray.pop(),
     myArray.pop(),
     myArray.pop(),
     myArray.pop(),
     myArray.pop()
    ]);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,141
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Try this as an alternative to rnd me's neat solution:-


    Code:
    <script type = "text/javascript">
    var myArray = []; 
     myArray[0] ="a";  // I assume these are phrases or sentences, not just letters
     myArray[1] ="b";
     myArray[2] ="c";
     myArray[3] ="d";
     myArray[4] ="e";
     myArray[5] ="f";
     myArray[6] ="g";
     myArray[7] ="h";
     myArray[8] ="i";
     myArray[9] ="j";
     myArray[10] ="k";
     myArray[11] ="l";
     myArray[12] ="a";
     myArray[13] ="b";
     myArray[14] ="c";
    
    var len = myArray.length;
    var chosen = [];
    
    for (var i = 0; i<=4; i++) {
    var randy = Math.floor(Math.random() * len);  
    chosen[i] = myArray[randy];
    
    for (k=0; k<=i-1; k++) {
    if (chosen[k] == chosen[i]) {
    i--;  // duplicate found so decrement i
    }
    }
    }
    
    alert (chosen);
    
    </script>
    Quizmaster: What is the more common name given to the aurora borealis?
    Contestant: Hmm, I'm not really a plant person.

  • #5
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much. It works really well.


  •  

    Posting Permissions

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