I'm sure that there is a function for that. I just want to make a list of 1000 numbers, each one of them which should be random.
相关问题
- Is “new” in Erlang part of the official standard a
- how to create a keep-alive process in Erlang
- ejabberd and Erlang installation with lager_transf
- Encrypt (cryptojs) - Decrypt (erlang)
- How to use erlang-examples
相关文章
- How do I modify a record in erlang?
- Check active timers in Erlang
- undefined function maps:to_json/1
- How to convert datetime() to timestamp() in Erlang
- what good orm api will work well with scala or erl
- GC performance in Erlang
- When to “let it crash” and when to defend the code
- Intercept login/logout ejabberd
Pseudorandom number generator from crypto module works better
crypto:rand_uniform(From, To)
.To generate a 1000-element list with random numbers between 1 and 10:
Make sure to seed appropriately.
Your intuition is that the results would be different. A random seed in Erlang is process specific. The default seed is fixed though. That's why you get the same result even though there are two processes in the example.
Note that if the return value of
now()
is the same in two different processes you end up with the same problem as above. Which is why some people like to use agen_server
for wrapping random number generation. Alternatively you can use better seeds.You need to correctly seed first of all.
You should check out Learn You Some Erlang which will guide you through the language.
To generate a 1000-element list with random numbers between 1 and 10:
Change 10 and 1000 to appropriate numbers. If you omit the 10 from from the
rand:uniform
call, you'll get a random floating point number between 0.0 and 1.0.On Erlang versions below 18.0: Use the
random
module instead. Caution! You need to runrandom:seed/3
before using it per process, to avoid getting the same pseudo random numbers.From Erlang Central wiki:
http://erlangcentral.org/wiki/index.php?title=Random_Numbers
Where N = no of items, StartVal = minimum value and Lim = maximum value