Steven Olmsted![]()
Y.WeightedList has many features similar to an array list and can generally function as one. Every value in a weighted list also has a weight value attached to it. Values can be retrieved from a weighted list by index as you would expect, but values can also be retrieved at random. The probability of any value being selected at random is weighted. For example, a value with a weight of 500 is twice as likely to be selected as a value with a weight of 250.
If gallery-alea is present, it will be used for random number generation instead of Math.random.
When the total sum of all weights in the list is 0, random selection will not work and will return null. Negative weights can yield unexpected results.
<script src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js"></script>YUI({
//Last Gallery Build of this module
gallery: 'gallery-2013.05.02-22-59'
}).use('gallery-alea', 'gallery-weighted-list', function(Y) {
// Create a new list.
var list = new Y.WeightedList(),
value;
// Add some weighted values to the list.
list.add('value 1', 5.5);
list.add('value 2', 10);
list.add('value 3', 2.5);
// Get a value randomly from the list.
value = list.value();
// There is about a 30% chance that value === 'value 1'.
// There is about a 56% chance that value === 'value 2'.
// There is about a 14% chance that value === 'value 3'.
});
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info