Jump to content

Random String Gen Enforcing at least 1 Numeric and 1 Alpha


kylomas
 Share

Recommended Posts

I wrote this as part of a routine to populate an Excel spreadsheet. I thought that I would post it just for shits and giggles...

The routine generates a variable number of variable length strings, each string containing at least one alpha and one numeric character.

This is slightly different as it uses a table of instructions that are randomly executed (as opposed to compound if...else statements).

#include <array.au3>

local $st = timerinit()
local $sStrings = gen_rnd_string(default,5000)
ConsoleWrite(round(timerdiff($st)/1000,3) & ' seconds' & @LF)

local $aStrings = stringsplit($sStrings,'|')
_arraydisplay($aStrings)

func gen_rnd_string($length = 8, $num = 1000)

    if $length = -1 or $length = default then $length = 8
    if $num    = -1 or $num    = default then $num    = 1000

    if not IsInt($length) then return seterror(1)
    if not IsInt($num)    then return seterror(2)
    if $length = 1        then return seterror(3)

    local $RndTbl[3] = ['chr(random(65,90,1))','chr(random(48,57,1))','chr(random(97,122,1))']

    local $str, $ret_str

    for $1 = 1 to $num
        for $2 = 1 to $length
            $str &= execute($RndTbl[random(0,2,1)])
        next
        ; enforce not all alpha and not all numeric
        if StringIsdigit($str) or StringIsAlpha($str) then
            $1 -= 1
        else
            $ret_str &= $str & '|'
        endif
        $str = ''
    Next

    return stringtrimright($ret_str,1)

endfunc

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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