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
not really efficient but will work:
more sophisticated:
one more:
Each of those solution has important faults: concurrent write performance, read overhead etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With