Is RANDOM command broken? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Is RANDOM command broken?

20 Mar 2018, 11:16

The following should, I think, generate random number from 0 to 2**31-1. It seems not too. Is my code incorrect or is RANDOM broken?

Code: Select all

a := ""
tot := 0
iter := 100
max  := 21474893647
loop %iter%
  {
  random, b, 0, %max%
  a .= " "b
  tot += b
  }

msgbox % "iteratinons: " iter  "`nMax:`t" max "`tAverage:`t"  tot/iter "`n"a
exitapp        
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Is RANDOM command broken?  Topic is solved

20 Mar 2018, 11:46

You specified 21474893647 as the max number but doc says:
The largest number that can be generated, which can be negative, floating point, or an expression. If omitted, the largest number will be 2147483647 (which is also the largest allowed integer value -- but floating point numbers have no restrictions).
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Is RANDOM command broken?

20 Mar 2018, 11:57

random has an upper range of (2^31)-1. the number youre feeding to random exceeds this upper limit, overflows a bunch of times and what ends up happening is youre running random with (0, 57177)
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Is RANDOM command broken?

20 Mar 2018, 12:07

yeah, thanks... I just noticed my typo (and extra 9 in the number).
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Is RANDOM command broken?

20 Mar 2018, 12:16

@swagfag : Out of curiosity, how do you get the number 57177 ? Since I ran the Op code ( several time with 10.000.000 iterations ) and the max number for me is 57167.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Is RANDOM command broken?

20 Mar 2018, 12:18

joefiesta wrote:yeah, thanks... I just noticed my typo (and extra 9 in the number).
What gives me the idea that c&p is our friend ;)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is RANDOM command broken?

20 Mar 2018, 13:08

Here we go:

Code: Select all

q:: ;overflow
;21474893647 = 0x50000DF4F ;typo
;2147483647 = 0x7FFFFFFF ;correct number

VarSetCapacity(vData, 4, 0)
NumPut(21474893647, &vData, 0, "Int")
MsgBox, % NumGet(&vData, 0, "Int") ;57167

;not quite the right calculation:
MsgBox, % Mod(21474893647, 2147483647) ;57177

;possibly the right calculation (although would give an unsigned integer, not a signed integer):
MsgBox, % Mod(21474893647, (2147483647+1)*2) ;57167
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Is RANDOM command broken?

20 Mar 2018, 13:15

Odlanir wrote:@swagfag : Out of curiosity, how do you get the number 57177 ? Since I ran the Op code ( several time with 10.000.000 iterations ) and the max number for me is 57167.
its a miscalculation on my part: i subtracted (2^31 - 1) a bunch of times, instead of just 2^31

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 219 guests