Jump to content

Whats Wrong With This Code


Recommended Posts

I want to make a 17 digit String. The digits can be Numbers or Letters.

I decided to use random heads/tails to decide if it will be a letter or a number.

Then it just needs to connect the new letter to the whole string.

example:

start with "" string

then first throw of the heads/tails = letter. Lets say that letter is 'A'.

So now the string should be "A"

Next roll a tails = Number. Lets Say # is 2.

So now the string should be "A2"

etc.

I tried useing C++ like syntax but it did not work. Anyway any advice would be cool. I use chr 64 to 90 because this is A to Z so it says in the help files.

$x = 18

While $x <> 0

$letter = 0

$var = String("")

If Random() < 0.5 Then ; Returns a value between 0 and 1.

$letter = 1

Endif

If $letter = 1 Then

$temp = Chr( Random(64, 90 ) + 1)

$var = ( $var + String($temp) )

Else

$temp = Int( Random(-1, 9) + 1)

$var = ( $var + String($temp) )

Endif

MsgBox(0, "var equals", "var: " & $var) ; just to see why this only returnes a 1 digit number.

$x = $x - 1

Wend

Send($var)

Link to comment
Share on other sites

  • Developers

this will reset it every loop back to nothing: $var = String("")

EDIT:

Heres a working version:

$x = 18
$var = ""
for $y = $x to 0 step -1
   If Random() < 0.5 Then
      $temp = Chr( Random(64, 90) + 1)
   Else 
      $temp = Int( Random(-1, 9) + 1)
   EndIf
   $var = $var & String($temp)
   MsgBox(0, "var equals", "var: " & $var); just to see why this only returnes a 1 digit number.
next
Send($var)
Edited by JdeB

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

hahahah oh my...yes that would do it. Lol. I ended up just editing that function that one guy showed me and got that working. But yeah that would of fixed the problem. I could of swore i had that outside of hte While ;-0.

here is the new function that does Uppercase letters and Numbers:

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("1"), Asc("9") + 1))

EndIf

$RESULT = $RESULT & $ALPHA

Next

Return $RESULT

EndFunc ;==>_RandomText

Its just that one guys code. Modified very slightly.

On the bright side i know a little about Functions now.

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