如何在Erlang中创建1000个随机数列表? [英] How to create a list of 1000 random numbers in Erlang?

查看:380
本文介绍了如何在Erlang中创建1000个随机数列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信有一个功能。我只想列出1000个数字,每个数字应该是随机的。

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.

推荐答案

具有1到10之间的随机数的元素列表:

To generate a 1000-element list with random numbers between 1 and 10:

[rand:uniform(10) || _ <- lists:seq(1, 1000)].

将10和1000更改为适当的数字。如果您从 rand:uniform 通话中省略了10个,则会得到0.0到1.0之间的随机浮点数。

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.

在低于18.0的Erlang版本中:使用随机模块。 注意!您需要在每个进程使用之前运行 random:seed / 3 ,以避免获得相同的伪随机数。

On Erlang versions below 18.0: Use the random module instead. Caution! You need to run random:seed/3 before using it per process, to avoid getting the same pseudo random numbers.

这篇关于如何在Erlang中创建1000个随机数列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆