The rand
function returns a number between 0
and RAND_MAX
. inclusive.
Divide this range into X sub-ranges, where X is the number of "variables" you have. If the returned value is inside the first sub-range use the first "variable". If the returned value is inside the second sub-range then select the second variable. Etc.
You can also use the modulo operator to get a value in the range from 0
to X, where X again is the number of "variables" you have. Then use this single number to select the "variable" (for example by using it as an index into an array).
This method using modulo is not perfect. See e.g. this old answer for why and for a better way.