Jump to content
Sign in to follow this  
Myicq

Fastest way to return random string, by a picture

Recommended Posts

Myicq

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.

Share this post


Link to post
Share on other sites
FireFox

@Myicq

There is already some examples to generate random strings and select the options you want.

The optimization (faster way to do it) has no sence until you want to generate "a ton" of chars.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
caleb41610

Try adding letters to an array... then call the array randomly. I'm sure that would be pretty quick.

Share this post


Link to post
Share on other sites
Myicq

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.

Share this post


Link to post
Share on other sites
FireFox

If it's around 0.1 second, critical.

Sure, you have the time to die.

Why?


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Robjong

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
  • Like 1

Share this post


Link to post
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
Sign in to follow this  

×