Modify

Opened 14 years ago

Closed 14 years ago

Last modified 13 years ago

#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 Changed 14 years ago by timsky

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

comment:2 follow-up: Changed 14 years ago by Valik

  • Resolution set to No Bug
  • Status changed from new to closed

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 Changed 14 years ago by Spiff59

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

comment:4 in reply to: ↑ 2 Changed 14 years ago by Antiriad

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 Changed 13 years ago by anonymous

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 Changed 13 years ago by anonymous

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

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.