Jump to content
Babu16

Challenge with Random Function

Recommended Posts

Babu16

Hi all,

I have written the following Random function to generate 8 character string, but the challenge is that in first iteration, this is generating as expected (8 character random string), from 2 iteration on wards, it's keep adding the previous strings, for example, first iteration (8 characters), 2nd iteration (16 characters) and keep on adding 8 until all iterations are complete.

But my expectation is to generate 8 characters random string on each iteration, Can you please help me on this?

 

For $k = 1 To 5
    RandomName()

Next

Func RandomName()
    Global $FirstName
    Global $LastName
    Global $aSpace[3]
    Global $digits = 8
    For $i = 1 To $digits
           $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z
         $FirstName &= $aSpace[Random(1)]

    Next
    MsgBox(0, "String Generator", "String Generated: Fname: " & $FirstName)
    For $j= 1 to $digits
              $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z
          $LastName &= $aSpace[Random(1)]

    Next
    MsgBox(0, "String Generator", "String Generated:LName: " & $LastName)

EndFunc

Share this post


Link to post
Share on other sites
Jos

You are defining the variables at the top of the Func as Global without initializing them, so they will survive the Func and not be reinitialized with the second call of the Func.

Make them local or initialize them like:  Global $FirstName = ""
Local is normally preferred.

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
jdelaney

Redefine the variables to empty in the loop.

Edit:  too slow

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
Babu16

Thank you, the problem has resolved by changing to Local

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

×