Jump to content

A better way of Randomising.


Recommended Posts

Hey Guys.

Happy Christmas and Merry new Year and all that..

I'm currently automating our entire system for end-2-end testing. I have about 12 Complex scripts all joined together in a GUI and it all works.

But I want to bring down the amount of lines of code that I used (I Started coding these script without any knowledge of coding/scripting so bear with me..)

Currently, I am generating a random number with the following code..

IniWrite(@ScriptDir & "\Iteration.ini ", "Boat Details", "Engine" , Random(111, 999, 1) & Chr(Random(Asc("A"), _
Asc("Z"), 1)) & Chr(Random(Asc("A"), Asc("Z"), 1)) & Random(111, 999, 1) & Chr(Random(Asc("A"), Asc("Z"), 1)) & _
Chr(Random(Asc("A"), Asc("Z"), 1)) & Chr(Random(Asc("A"), Asc("Z"), 1)) & Chr(Random(Asc("A"), Asc("Z"), 1)) & _
Random(111, 999, 1) & Chr(Random(Asc("A"), Asc("Z"), 1)) & Chr(Random(Asc("A"), Asc("Z"), 1)))

Please note that the amount of characters is critical for my tests.

Now the question, Is there a better way of doing this?

Thanx for the assist.

Kama

Link to comment
Share on other sites

Kamakazy,

If I was going to do something like that I would structure it like this

IniWrite( @ScriptDir & "\Iteration.ini ", "Boat Details", "Engine" , _num() & _
                                                                     _chr() & _
                                                                     _chr() & _
                                                                     _num() )   ; etc for as long as you require


func _chr()
    return Chr(Random(Asc("A"), Asc("Z"), 1))
EndFunc

func _num()
    Random(111, 999, 1)
endfunc

just for readability.

kylomas

edit: it does NOT bring down the # of lines of code but it's a hell of a lot easier to read. The # of lines of source should be considered only after readability and maintainability, if at all.

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

func _num()
Return Random(111, 999, 1)
endfunc

will work even better ;-)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

This is a pretty easy error to make and AutoIt won't bark:

Local $a = fct()

Func fct()
Random(0, 9, 1) ; or whatever without Return
EndFunc

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×
×
  • Create New...