I want to pick a random number from a list of integers in a MySQL Stored Procedure. This list is generated from another function using some calculations.
Example:
[41,69,9,31,10,33,13,73,20,62,21,58,22,39]
I tried the RAND()
function but they are dependent on constant min, max, and step values it seems. Any help would be great.
Note:
This list of numbers is not always constant, so they do not have constant min, max, or step values.
2
Answers
You can use the RAND() function along with the COUNT() function to select a random element from your list of integers.
Here is an example Stored Procedure that demonstrates how you can do this:
You can call this Stored Procedure using the following query:
This will return a random integer from your list of integers. Note that you will need to update the value of the list variable in the Stored Procedure to match your own list of integers.
There are two functions that can be used. Try using
elt()
function.