Negative random numbers

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
``````
• randomNum = (7-(-8))*prng.rand(0..1) + (-8) Wouldn't this generate a with 50% chance ? (when you have 0 with prng.rand(0..1) – m_vdbeek Oct 28 '11 at 17:20
• @Awake yes you are correct, use prng.rand instead to generate a random uniform number betwen 0 and 1 – Pepe Oct 28 '11 at 18:48

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

• The only rubyesque answer... Maybe add since which ruby version this is possible. – count0 Jan 29 '18 at 21:05

How about you multiply the positive random number by -1

• yes but you will get 100% of negative numbers what i want to do is balance between positive / negative numbers if I do prng.rand(-10..10). But this shouldn't work because of the ArgumentError no ? – m_vdbeek Oct 28 '11 at 17:17
• then go 0...10 and subtract 20! – Mörre Oct 29 '11 at 5:23
``````def rand(a, b)
return Random.rand(b - a + 1) + a
end
rand(-3, 5)
``````
• thanks a lot ! i will test it tonight ! – m_vdbeek Oct 28 '11 at 17:27
• Would not this fail if both a and b are negative? – Andrey Oct 28 '11 at 17:37
• Hmm, I just tested with rand(-9, -5) and it seems to work properly yet. Just have to make sure -9 is first as it's the lesser of the two. – Steven Jackson Oct 28 '11 at 17:39
• If you do rand (-9, -5) you get Random.rand(-5-(-9)+1)+(-9) = Random.rand(5)-9 ... if rand returns 5 than 5-9=-4 and that is not in the range of (-9, -5) – Andrey Oct 28 '11 at 17:44
• Ok, so is there some solution to this problem ? – m_vdbeek Oct 28 '11 at 17:59

I needed to generate a random integer, either 1 or -1 and I used this:

``````prng = Random.new(1)
prng.rand > 0.5 ? 1 : -1
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.86255354,"math_prob":0.9719662,"size":1214,"snap":"2019-26-2019-30","text_gpt3_token_len":366,"char_repetition_ratio":0.16198348,"word_repetition_ratio":0.2882883,"special_character_ratio":0.31383854,"punctuation_ratio":0.14487633,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99622095,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-06-24T14:45:18Z\",\"WARC-Record-ID\":\"<urn:uuid:31a1307a-1909-4809-bdd2-f941a5cce734>\",\"Content-Length\":\"153900\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:56482d8d-60ce-4dad-ad27-20771373f9ff>\",\"WARC-Concurrent-To\":\"<urn:uuid:0e7420b2-cb64-463c-afc2-83faa3f5aedc>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/7932493/negative-random-numbers/7932511\",\"WARC-Payload-Digest\":\"sha1:34J53UI3MIPF5JAE76QNOKFBJ7EPMGNQ\",\"WARC-Block-Digest\":\"sha1:DFB2VFRGXTFBPMW2WBDBCYQUZSWUC6TB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-26/CC-MAIN-2019-26_segments_1560627999539.60_warc_CC-MAIN-20190624130856-20190624152856-00407.warc.gz\"}"}