I have an mnesia table t
that contains records with a single field x
. How can I select a random value x
from t
?
To avoid the entire of process of mathematical pedantry: I don't care about the details of the random number generation, I just want my result to generally not be the same every time.
Thanks,
-tjw
By using the mnesia:all_keys/1
(or dirty equivalent) function and the random
module.
random_value(Table) ->
Keys = mnesia:dirty_all_keys(Table),
Key = lists:nth(random:uniform(length(Keys)), Keys),
[#record{x = X}] = mnesia:dirty_read({Table, Key}),
X.
Don't forget to initialize your seed using random:seed/3
.