utcghghvgh - 1 year ago 59

Ruby Question

I am looking for a function which takes an input number and returns a number between two numbers

`def generate_number input_number, max, min`

end

The output of the function should be a number dependent on the

`input_number`

Note: Everytime it should return the same output for the

`input_number`

Answer Source

You are looking for `Random`

:

```
Random.new(123).rand(1..10) #=> 3
```

`123`

is the seed (your *input_number*), `1`

and `10`

your *min* and *max* values.

Different seeds generate different numbers, same seeds result in identical numbers. This is especially useful when generating a repeatable series of random numbers:

```
r = Random.new(123)
10.times.map { r.rand(1..10) }
#=> [3, 3, 7, 2, 4, 10, 7, 2, 1, 2]
r = Random.new(456)
10.times.map { r.rand(1..10) }
#=> [6, 10, 5, 6, 8, 2, 9, 4, 6, 3]
r = Random.new(123)
10.times.map { r.rand(1..10) }
#=> [3, 3, 7, 2, 4, 10, 7, 2, 1, 2]
```