Modify

Opened 12 years ago

Closed 12 years ago

#2686 closed Feature Request (Completed)

Random to return max value when min equals max.

Reported by: anonymous Owned by: Jon
Milestone: 3.3.13.10 Component: AutoIt
Version: Severity: None
Keywords: Random Cc:

Description

This has been rejected previously but I don't think it was fully justified. With the Random function, I suggest returning the maximum value when it is equal to the minimum value, regardless of whether you set the error flag or not. I don't personally think it should be considered an error, but maybe removing the error will break too many scripts.

Hopefully the positive outcome of such a small change would outweigh any negatives. The following code is nice and easy regatds syntax, but it currently breaks on the final iteration.

For $i = $iMin To $iMax
	$iRandom = Random($i, $iMax, 1)
Next

Attachments (0)

Change History (9)

comment:1 by czardas, 12 years ago

Forgot to include the author - cardas

comment:2 by jchd18, 12 years ago

This should file under Bugs instead of Feature request!

Any script relying on Random($n, $n, 1) with $n an integer raising an error is broken, by the mere mathematical definition of the interval [n, n], n ∈ ℕ because [n, n] = {n}, the singleton n.

I don't know how many times this must be stressed before someone engages brain.
Pick a random card out of a shuffled deck of 52 cards: OK.
Pick a random card out of the remaining 51 cards: OK.
. . .
Pick a random card out of the remaining 2 cards: OK.
Pick a random card out of the remaining 1 card: ERROR, then sing 'God save the Queen' loud?
Ridiculous.

comment:3 by anonymous, 12 years ago

If someone wishes to move it to bugs, then fine. It was a choice decision, so I never thought of it as a bug.

comment:4 by czardas, 12 years ago

You can treat it as an error and still return a value within the infinitesimal range between min and max.

comment:5 by Melba23, 12 years ago

Strongly supported. The argument that the return if "Min = Max" is not truly random is specious - as jchd has demonstrated.

I am less in favour of the suggestion that @error should remain set to maintain backwards compatiblity - although it could act as a flag indicating that the "Min = Max" condition exists. But the main return should certainly be the value of the 2 equal limits.

M23

comment:6 by jchd18, 12 years ago

Should a condition denote equality of bounds be really necessary (which I find doubtful, since its way clearer to use an If or a ternary construct to explicitely test for that), then it should be set in @extended and not in @error. That's because no error occured in this situation.

@Extended would then mean "randomlessness" — if we dare coining the term — a perfectly suitable use for @extended. This wouldn't be script breaking but script engine fixing. Anyone claiming otherwise is in a state of sin.

comment:7 by czardas, 12 years ago

I'm glad you guys are considering this. I also thought of getting rid of max and min. I don't see why the numbers can't appear in any order. If that's complicating matters too much, then it's easy to work around anyway.

comment:8 by anonymous, 12 years ago

... reluctance of developers to improve the function forces us to write the wrapper such as:

Func _random($min = 0, $max = 1, $flag = 0)
	If $min >= $max Then Return SetError(1, 0, $min)
	If @NumParams = 1 Then Return Random(0, $min, $flag)
	Return Random($min, $max, $flag)
EndFunc   ;==>_random

I think that most do similar in your scripts.

comment:9 by Jon, 12 years ago

Milestone: 3.3.13.10
Owner: set to Jon
Resolution: Completed
Status: newclosed

Added by revision [10464] in version: 3.3.13.10

Modify Ticket

Action
as closed The owner will remain Jon.

Add Comment


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