3
votes

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.

2
You're on the right track, have a look at the function randRange within the structure Random, it should be a perfect fit for what you need - waldrumpus
@waldrumpus Hmm, alright, but I don't understand how to write the arguments. It should be something like Random.randRange((i, j) s), (or I'm completley wrong) but what is s? - froli
Use the function Rand.rand : (int * int) -> rand to instantiate a random number generator. Also, be careful to leave out the enclosing parentheses when calling a curried function: Random.randRange (i, j) s - waldrumpus
@waldrumpus In Poly/ML there is no support for Random structure. How shall this be done with Poly/ML? - coffeMug

2 Answers

5
votes

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;
0
votes

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.