Jump to content

5 Digit Number Generator Problem


Recommended Posts

added probability check

#include <Array.au3>
Local $iNumbers
Local $iDigits = 3
Local $t = TimerInit()
For $n = 0 To 1000
_RandomEx($iNumbers, $iDigits); Randomize without Repetition
Next
ConsoleWrite('Timer:' & TimerDiff($t) & @CR)
_ArrayDisplay($iNumbers, 'RandomEx | Phoenix XL [' & @ScriptName & ']')

Func _RandomEx(ByRef $aNumbers, $nDigits = 5);Converted from Recursive to Iterative
;All the Possible Numbers have been added return -1 and set @error
If UBound($aNumbers) >= ((10 ^ $nDigits) - 1) - (10 ^ ($nDigits - 1)) + 1 Then Return SetError(1, 0, -1)
Local $vTemp, $nChk = 0
While 1
$vTemp = Random(10 ^ ($nDigits - 1), (10 ^ $nDigits) - 1, 1)
_ArraySearch($aNumbers, $vTemp)
If @error Or $nChk >= (10 ^ ($nDigits/2) ) - 1 Then ExitLoop
$nChk += 1
WEnd
If $nChk >= (10 ^ $nDigits) - 1 Then $vTemp = _ProbabilityCheck($aNumbers, $nDigits)
Switch IsArray($aNumbers)
Case 0
;Make it an Array
Local $n[1] = [$vTemp]
$aNumbers = $n
Case 1
_ArrayAdd($aNumbers, $vTemp)
EndSwitch
Return $vTemp
EndFunc ;==>_RandomEx

Func _ProbabilityCheck(ByRef $aNumbers, $nDigits)
For $n = 10 ^ ($nDigits - 1) To (10 ^ $nDigits) - 1
_ArraySearch($aNumbers, $n)
If @error Then
_ArrayAdd($aNumbers,$n)
Return $n;
EndIf
Next
Return -1
EndFunc ;==>_ProbabilityCheck

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Just a general random thought:

Think that in general Random() should not be used to generate separate unique numbers on the fly. If the generated amount of numbers will covers more than 50% of the target number set/range (personally I would stop around 25%). In those cases czardas method in post #5 should be the way to go. CQ. Using random() to randomize the whole sequential number set, and then picking off the numbers one by one of that list (Smartly used it can be made to handle pretty big number set's. ... If needed).

For additional code examples ... Taking it to the example section probably not a bad idea.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...