Jump to content

Challenge with Random Function


Babu16
 Share

Recommended Posts

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

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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