Generating a random number in SML

折月煮酒 提交于 2021-01-27 16:00:39

问题


How can you generate a random number from a specific range, for example the integer 34 in the range [1, 100]?

I looked at the Random structure but it doesn't give me what I want, at least from what I can understand.


回答1:


I think you have to use the Random structure in the given link like this ...

- val nextInt = Random.randRange (1,100);
- val r = Random.rand (1,1);
- val x1 = nextInt r;
- val x2 = nextInt r;



回答2:


To get 34 integers between 1 and 100, you could use:

let
    val seed = Random.rand (123,456)
in
    List.tabulate(34, fn i => Random.randRange (1,100)  seed)
end;

Note that the value seed is a reference that gets updated with each call to Random.randRange.



来源:https://stackoverflow.com/questions/22067535/generating-a-random-number-in-sml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!