inherit
203080
0
Mar 17, 2014 14:50:56 GMT -8
baratheon
43
December 2013
baratheon
|
Post by baratheon on Dec 28, 2013 10:02:48 GMT -8
I have a javascript array() with the names of the users as keys, and the number of entries they've submitted to a raffle as values. Based on that, I need to somehow select a random winner. At the moment I'm using something like this:
var rand_num = Math.floor(Math.random()*all_entriesCount);
var counter = 0;
$.each(all_entries, function(key, val){
if(counter == rand_num){
//set winner return false; //break out of loop }
counter++;
});
But it doesn't account for the number of entries a user submitted. I'd like it so, the more entries a user submits, the higher his probability of winning. I'm guessing one way to do this would be to add another loop inside of the $.each loop?
Any ideas?
|
|
inherit
The Great Cinnamon Roll
191518
0
Oct 19, 2016 22:17:44 GMT -8
David Clark
Care for some tai chi with your chai tea?
17,602
March 2013
davidlinc1
|
Post by David Clark on Dec 28, 2013 10:49:42 GMT -8
Since this is a code-related inquiry, I'll push this thread into the Plugin Development sub-board
|
|
inherit
King Oligochaete
126470
0
Feb 24, 2021 12:23:15 GMT -8
Wormopolis
Stop rating plugins with 1 star that dont deserve it.
20,002
June 2008
tbserialkillers
Wormo's Mini-Profile
|
Post by Wormopolis on Dec 28, 2013 13:26:15 GMT -8
in the past, whenever I needed to randomly pick from a weighted array, a less elegant solution would be to create a new array that contained multiple entries for weighted array entries. like if an entry had 3 times the chance it would be in the new array 3 times. then take your random number from the length of that array.
there are more elegant solutions that can probably be found on google.
|
|