I am using the following to generate a near random number.
3> erlang:ref_to_list(make_ref()).
"#Ref<0.0.0.36>"
What I want is 00036
Well it was what I had been informed I could do in a previous post. It occurred to me that it is not so easy to extract the numbers from make ref.
Can anyone show how it is easily done, or possibly recommend another solution.
Keep in mind that using random:seed() is not random when called within the same few nano seconds.
Regards
Note: from OTP 18
erlang:now/0andrandommodule are deprecated, and OTP 20 will remove therandommodule. See Time and Time Correction in Erlang for the further details. Also, you no longer need to do the per-process seeding if you userand:uniform/0. The following is left as is for reference.The problem is that you are using
randomincorrectly.random:seed/0will seed the random number generator with the very same seed always. This is not good for what you want. Rather, you can userandom:seed(erlang:now())to seed it with another number, namely the current time.“What happens if two calls come very close?” you may ask. Well, the Erlang guys thought about this, so
now/0is guaranteed to always return increasing numbers:(emphasis mine)
Also note that the
randomPRNG is per-process, so you should always start your process up with a seeder call:Using references for this is perhaps possible, but I don’t think it is a viable one. The solution goes over
erlang:ref_to_list/1and it is not pretty.