Myicq Posted October 26, 2012 Share Posted October 26, 2012 (edited) 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.xa=any letter a-zA=any letter A-Z0 = any number 0-91 = any number 1-9! = any punctuation from a listThe 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 endfuncMy 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 October 26, 2012 by Myicq I am just a hobby programmer, and nothing great to publish right now. Link to comment Share on other sites More sharing options...
FireFox Posted October 26, 2012 Share Posted October 26, 2012 @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. Link to comment Share on other sites More sharing options...
caleb41610 Posted October 26, 2012 Share Posted October 26, 2012 Try adding letters to an array... then call the array randomly. I'm sure that would be pretty quick. Multi-Connection TCP Server Link to comment Share on other sites More sharing options...
Myicq Posted October 26, 2012 Author Share Posted October 26, 2012 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 More sharing options...
FireFox Posted October 26, 2012 Share Posted October 26, 2012 If it's around 0.1 second, critical.Sure, you have the time to die.Why? Link to comment Share on other sites More sharing options...
Robjong Posted October 26, 2012 Share Posted October 26, 2012 (edited) 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 October 26, 2012 by Robjong Myicq 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now