Modify

#1538 closed Bug (No Bug)

Random(1, 1, 1) = 0

Reported by: timsky Owned by:
Milestone: Component: AutoIt
Version: 3.3.6.0 Severity: None
Keywords: Random Cc:

Description

AutoIt 3.3.6.0
Windows 7 x64 En

First ticket was closed with resolution that this is not a bug:
http://www.autoitscript.com/trac/autoit/ticket/1170

Second ticket was closed with words that this bug is fixed in latest beta:
http://www.autoitscript.com/trac/autoit/ticket/1251

Now on latest AutoIt version 3.3.6.0 I get this bug (again?):

$rnd = Random(1, 1, 1)
MsgBox(0, @error, $rnd)

I think Random generator should return a number if Min and Max are equal. I mean Random(10, 10, 1) should return 10 because sometimes Min or Max or both of them are dynamically changed during script execution.
Also when Random() returns 0 this might lead to error(s) if coder will not forget to add error handling routine.

Thank you!

Attachments (0)

Change History (6)

comment:1 by timsky, on Mar 26, 2010 at 2:04:18 AM

Also Min and Max can depend on external factors like user input or number of processes/windows/lines of text etc...

comment:2 by Valik, on Mar 28, 2010 at 7:46:29 PM

Resolution: No Bug
Status: newclosed

This can be easily fixed by a line of code:

$nResult = Random($nMin, $nMax, 1)
If @error Then $nResult = $nMin

The excuse that you might forget to write error-handling code is not a good one.

I'm closing this as no bug. I'm not sure about my comments in #1251 because at that time AutoIt had some bugs in it that may or may not have influenced the output of Random().

comment:3 by Spiff59, on Apr 22, 2010 at 7:21:33 PM

It's a shame that we have to employ additional coding to handle the illogical and undocumented behavior of this routine.

in reply to:  2 comment:4 by Antiriad, on Aug 31, 2010 at 3:27:24 PM

The excuse that you might forget to write error-handling code is not a good one.

And I find the response 'we dont need to fix the bug because you can implement error handling to check for the bug and workaround it' an even poorer excuse to not fix it!

Either the bug needs fixing or the documentation should be updated to state the numbers must not be equal. This has caused me a good deal of grief before discovering this undocumented 'feature'.

comment:5 by anonymous, on Oct 14, 2011 at 3:57:44 PM

I also had the pleasure of discovering this undocumented 'feature'.

I use this in my functions from now on

; This overcomes the stupidity and undocumented behavior of Random()'s return value
; If (MIN = MAX) Then return MIN (whereas Random() always returns 0)
Func _Random($nMin = 0, $nMax = 1, $nInt = 0)
	Local $nResult = Random($nMin, $nMax, $nInt)
	If @error Then Return SetError(1, 0, $nMin)
	Return $nResult
EndFunc	;==>_Random

comment:6 by anonymous, on Oct 14, 2011 at 4:06:22 PM

Better yet:

; This overcomes the stupidity and undocumented behavior of Random()'s return value
; If (MIN = MAX) Then return MIN (whereas Random() always returns 0)
Func _Random($nMin = 0, $nMax = 1, $nInt = 0)
	Local $nResult
	If @NumParams = 1 Then; If only one argument is provided, then it is interpreted to be the Max.
		$nResult = Random($nMin)
	Else
		$nResult = Random($nMin, $nMax, $nInt)
	EndIf
	If @error Then Return SetError(1, 0, $nMin)
	Return $nResult
EndFunc	;==>_Random

Modify Ticket

Action
as closed The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.