Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-05-2010, 07:55 PM   PM User | #1
mazdaspring
New to the CF scene

 
Join Date: Sep 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mazdaspring is an unknown quantity at this point
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")
mazdaspring is offline   Reply With Quote
Old 09-05-2010, 08:12 PM   PM User | #2
jimhill
Regular Coder

 
Join Date: Jul 2010
Posts: 271
Thanks: 3
Thanked 40 Times in 40 Posts
jimhill is an unknown quantity at this point
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.
jimhill is offline   Reply With Quote
Old 09-05-2010, 09:06 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is online now   Reply With Quote
Old 09-05-2010, 10:36 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 09-07-2010, 10:17 PM   PM User | #5
mazdaspring
New to the CF scene

 
Join Date: Sep 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mazdaspring is an unknown quantity at this point
Thank you so much. It works really well.
mazdaspring is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:06 PM.


Advertisement
Log in to turn off these ads.