Jump to content

Random Text Udf


ezzetabi
 Share

Recommended Posts

Nothing difficult... if ever need a password generator, a way to make temp file with out worry too much about files that already exists or what ever you can use this...

As usual, append to the end of file and call it :D

E.G.

;Creates the temp file for text operations

$TempFile = @TempDir & "\" & _RandomText(10) & ".tmp"

$TempFile = FileOpen($TempFile)

Func _RandomText($N)
 ;$n is the lenght of string.
   If $N < 1 Then Return -1
   Local $COUNTER, $ALPHA, $RESULT
   
   For $COUNTER = 1 To $N
      If Random() < 0.5 Then
         $ALPHA = Chr(Random(Asc("A"), Asc("Z") + 1))
      Else
         $ALPHA = Chr(Random(Asc("a"), Asc("z") + 1))
      EndIf
      $RESULT = $RESULT & $ALPHA
   Next
   Return $RESULT
EndFunc  ;==>_RandomText
Link to comment
Share on other sites

Didn't I put something like this in the help file for Random?

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

well, it does seem to appear that the middle section of that code is in the help file, though a script-diot like me would not easily make the jump to what ezzetabi posted here...

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

so, slap this in front of the random text code and you have a random password generator (though who would use these passwords?)

$char = InputBox("Random Password","How many characters?", "10")
InputBox("Random Password","Here you go:", _RandomText($char))

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

heh, yeah, I guess I could use them when a user forgets their password, it'd garuntee that they reset it to something else pdq

"I'm not even supposed to be here today!" -Dante (Hicks)

Link to comment
Share on other sites

  • 2 weeks later...

Since I actually found a useful use of it (random passwords), see encryption thread. I improved it :D

Now adds also numbers or write spaces.

The probability are calculated counting the number of characters.

total chars=63, 26 maiusc, 26 min, 10 digits, 1 space

10/63 = 15.87 % and so go on...

Func _RandomText($N)
  ;$n is the lenght of string.
   If $N < 1 Then Return -1
   Local $COUNTER, $ALPHA, $RESULT, $RANDOM
   
   For $COUNTER = 1 To $N
      $RANDOM = Random()
      Select
         Case $RANDOM < .1587
            $ALPHA = Chr(Random(Asc("0"), Asc("9") + 1))
         Case $RANDOM < .5714
            $ALPHA = Chr(Random(Asc("A"), Asc("Z") + 1))
         Case $RANDOM < .9841
            $ALPHA = Chr(Random(Asc("a"), Asc("z") + 1))
         Case Else
            $ALPHA = " "
      EndSelect
      $RESULT = $RESULT & $ALPHA
   Next
   Return $RESULT
EndFunc  ;==>_RandomText
Link to comment
Share on other sites

I should check the forum more often, i jut wrote a similar function :huh2:

Func Uniqid()
    Local $i, $rnd, $rval
    For $i = 1 To 64
  $rnd = Round(Random(1, 3), 0)
  Select
    Case $rnd == 1;uc (65-90)
    $rval = $rval & Chr(Round(Random(65, 90), 0))
    Case $rnd == 2;lc (97-112)
    $rval = $rval & Chr(Round(Random(97, 112), 0))
    Case $rnd == 3;nr (48-57)
    $rval = $rval & Chr(Round(Random(48, 57), 0))
  EndSelect
    Next
    Return $rval
EndFunc  ;==>Uniqid

i think i'm going to benchmark them and see witch one is faster :D

Edit: And The winner is _RandomText witch generates about 33000 random strings more per hour :)

But why this +1 thing?

Edited by piccaso
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

But why this +1 thing?

Just because Random($min,$max) do not include max in the results. So I have to keep $max one higher than the max number (that is Z, z or 9 char number)

Since you used Round(), you do not need this, all numbers greater than max - 0.5 will be rounded to max.

But I think is redundant force the computer round a number every time when the function already take Int(s) number and you can avoid the problem adding 1.

Edited by ezzetabi
Link to comment
Share on other sites

though who would use these passwords?

Actually, random passwords like this are a very good idea :) .. And not only for clever file-trashing ..

There are commercial applications (and even freeware) that allow you to store your logins and passwords in a single database.

=> This means all you have to remember is a single password - the one for the database.

=> Which in turn means you can improve the strength of the passwords you store in it .. even to the point of making them obscure and difficult for anyone to remember: by using a password generator.

So you end up with good strong passwords (unique, mixed case, even including numerics and funny chars), without having to remember them. :huh2:

As I write this, I'm considering an AU3 applet to do just this, using the IDEA encryptor that ezzetabi posted to protect the database. Someone else might have the time and interest too :D

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...