Pick random element: Difference between revisions

Content added Content deleted
Line 9: Line 9:


=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
The easiest way to pick a random element from a list is to use a random number generator's output as an index into an array. Care must be taken not to index out of bounds. If the array's size is a power of 2, then <code>RNG output & (array size - 1)</code> will ensure that the array is not indexed out of bounds while maintaining the randomness of the selection. Otherwise, you will have to manually check the RNG output against the array's size and roll again if the RNG output is larger.
The easiest way to pick a random element from a list is to use a random number generator's output as an index into an array. Care must be taken not to index out of bounds. If the array's size is a power of 2, then <code>index = RNG output & (array size - 1)</code> will ensure that the array is not indexed out of bounds while maintaining the randomness of the selection. Otherwise, you will have to manually check the RNG output against the array's size and roll again if the RNG output is larger.


For brevity's sake, the implementations of the RNG and printing routines were left out; they can be provided if requested.
For brevity's sake, the implementations of the RNG and printing routines were left out; they can be provided if requested.