In JS you could use this and call it a day.
let randomArrayEntry = myArray[Math.floor(Math.random() * myArray.length)];
However, in Solidity everything is deterministic unless you call an outside function for a random number. Easy in theory, but the problem is that it is not currently practical with available services to choose a number matching your array length.
If my array length is 23, and the oracle spits out 1823792836199027320224 as the seed we can reduce it down to the last two numbers % 10**2
. Now we have the number 24 and we now have a range from 0-99 of possible numbers. How can we then choose fairly from the array using this number without spending a bunch of gas iterating over it?
The first solution I came up with is to check for the first two numbers in a row less than 24, and then that becomes the answer. The problem is that the randomly generated number might not have any two numbers in a row less than our array length. This becomes much more likely if our number is 2 or 3. The transaction would have to revert or fetch another random number as a fallback. This seems like a poor plan.
Edit 1:
The current solution (found after) is to just take the remainder.
uint256 randomArrayEntry = (randomNumber + myArray.length + 10) % myArray.length;
You multiply the random number by 10 so you don't divide zero or less than the array length (no remainder).
oracleOutput % 23
.