Jump to content

Fastest way to return random string, by a picture


Myicq
 Share

Recommended Posts

I would like to create random strings, for a project.

For the flexibility I would like to give user option to specify a picture with placeholders, f.x

a=any letter a-z

A=any letter A-Z

0 = any number 0-9

1 = any number 1-9

! = any punctuation from a list

The core function would be something like

; Read picture from ini file
$pict = iniread("setup.ini", "setup", "pict", "a")
$result = makerandom($pict)

func makerandom($p)
;
; something here to create string
;
return $mystring
endfunc

My initial approach would be to iterate over chars in $p using an if or case - but which is faster ? Is there another and better approach ?

I found this DLL - perhaps it could be be used (just need to study hard how ;) )

Edited by Myicq

I am just a hobby programmer, and nothing great to publish right now.

Link to comment
Share on other sites

Think I will have to time the process. If it's in the range of 0.001 seconds or less, not important. If it's around 0.1 second, critical.

I will post results.

I am just a hobby programmer, and nothing great to publish right now.

Link to comment
Share on other sites

Edit: I didn't understand the question correctly on the first read, and removed my post, but maybe it is something you or anyone finding this post can use so here it is...

Generating random strings:

#region - EOU
$iLength = 12
$iLoops = 1000

$t = TimerInit()
For $i = 1 To $iLoops Step 1
    $s = _StringRand($iLength)
    ConsoleWrite("S " & $i & " : " & $s & @LF)
Next

ConsoleWrite($iLoops & " random strings of " & $iLength & " characters : " & (TimerDiff($t) / 1000) & " seconds" & @LF)
#endregion - EOU


; #FUNCTION# ====================================================================================================================
; Name ..........: _StringRand
; Description ...: Generate a random string of a given length.
; Syntax ........: _StringRand($iLen[, $sChars = Default])
; Parameters ....: $iLen             - The length of the random string to generate.
;                 $sChars          - [optional] A string of characters to pick from then generating a random string.
; Return values .: The generated random string.
; Author ........: Robjong
; Modified ......:
; Remarks .......:
; ===============================================================================================================================
Func _StringRand($iLen, $sChars = Default)
    If Not $sChars Or $sChars == Default Then $sChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    If Not IsString($sChars) Or StringLen($sChars) <= 1 Then
        Return SetError(1, 0, 0)
    EndIf
    Local $sRand = "", $iMax = StringLen($sChars)
    For $i = 1 To $iLen
        $sRand &= StringMid($sChars, Random(1, $iMax, 1), 1)
    Next
    Return SetError(0, 0, $sRand)
EndFunc   ;==>_StringRand

and these are the times I got:

; 1000 random strings of 6 characters : 0.0827935853563691 seconds (1 = 0.0000827935853563691)
; 1000 random strings of 12 characters : 0.0850768985056386 seconds (1 = 0.0000850768985056386)
; 1000 random strings of 32 characters : 0.178991956002444 seconds (1 = 0.000178991956002444)

For the a=a-z etc. look at the 2nd parameter of _StringRand.

Edited by Robjong
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...