Requirement:
1. Only PHP is allowed
2. It can be either a function or a class
3. the input array of string is a numbered array, each one stores a string. It's created by using:
4. The input array of weight is also a numbered array, for a $weight[$i], it resemble the weight value of $array[$i]. $weight contain only positive integers. $weight[$i] does not need to be created, if there is no $weight[$i] match $array[$i], the weight 1 will be used
5. Return one of the string of the $array dependent on the $weight
Note: in case you don't know what is weighted random. two or more items, each have a weight, weight decide the chance it get chosen. A item with weight 5 have 5 times more chance to be chosen than a item with weight 1, 2.5 times more chance than a item with weight 2. let me give you an example of what it is like:
Final Test for speed:
There will be three test it have to go though, here are the inputs for each:
Test 1:
Test 2:
Test 3:
Winner is the person who have the smallest number after the weighted number of all tests, which is
Final result for test 1 *2 + Final result for test 2 * 5 + final result for test 3 * 3
The Winner will get all my $F
Note: When would people need weighted random?
Most likely in games. Kill a monster, what will be dropped? Diamond weight 1 and wooden sword weight 1000000, then most likely you get the wooden sword.
1. Only PHP is allowed
2. It can be either a function or a class
3. the input array of string is a numbered array, each one stores a string. It's created by using:
| Code: |
| $array[] = 'string'; |
4. The input array of weight is also a numbered array, for a $weight[$i], it resemble the weight value of $array[$i]. $weight contain only positive integers. $weight[$i] does not need to be created, if there is no $weight[$i] match $array[$i], the weight 1 will be used
5. Return one of the string of the $array dependent on the $weight
Note: in case you don't know what is weighted random. two or more items, each have a weight, weight decide the chance it get chosen. A item with weight 5 have 5 times more chance to be chosen than a item with weight 1, 2.5 times more chance than a item with weight 2. let me give you an example of what it is like:
| Code: |
| $array[] = 'weight 1';
$array[] = 'weight 2, have higher chance to be chosen than weight 1'; $array[] = $array[1]; echo $array[array_rand($array)]; |
Final Test for speed:
There will be three test it have to go though, here are the inputs for each:
Test 1:
| Code: |
| $array[] = 'test for speed of no weight arrays';
$array[] = 'how fast is this'; $array[] = 'the final result for this * 2'; |
Test 2:
| Code: |
| $array[] = 'test for weight';
$weight[] = 30000; $array[] = 'with HUGE numbers'; $weight[] = 2000; $array[] = 'final result for this * 5'; $weight[] = 50000; |
Test 3:
| Code: |
| $array[]= 'test for weight array only on some';
$weight[] = 20000; $array[]= 'final result for this * 3'; |
Winner is the person who have the smallest number after the weighted number of all tests, which is
Final result for test 1 *2 + Final result for test 2 * 5 + final result for test 3 * 3
The Winner will get all my $F
Note: When would people need weighted random?
Most likely in games. Kill a monster, what will be dropped? Diamond weight 1 and wooden sword weight 1000000, then most likely you get the wooden sword.
