I know how to generate numbers with Rails but I don't know how to generate negative numbers?
prng.rand(1..6) for random of [1, 2, 3, 4, 5, 6]
Random doc says that you would get an ArgumentError.
Let's say you want to generate a number between a and b you can always do that using this formula:
randomNum = (b-a)*prng.rand + a
So if you want a number between -8 and +7 for example then a=-8 and b=7 and your code would be
randomNum = (7-(-8))*prng.rand + (-8)
which is equivalent to
randomNum=15*prng.rand - 8
Suppose you want to generate negative numbers between -1 to -5
You can simply do this:
rand(-5..-1)
It will generate random numbers between -5 to -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With