Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
|
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 22nd, 2009, 06:43 AM
Authorized User
 
Join Date: Apr 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Question 2 Separate Questions For The Same Code

The purpose of this Code is to test the Random Number Generation Process. I am not interested in going through 5 different Message Boxes; but, want all 5 Generated Random Numbers to be listed within the same Message Box.

I want the Random Numbers Generated to be different each and every time this Code is RUN.

I have been successful in generating a different sequence of Random Numbers during each Runtime; but, I haven't been successful at displaying the 5 Random Numbers Generated within a single Message Box.

QUESTION #1
?Why does the following Code only produce 4 Random Numbers instead of 5 within the Message Box?

?What do I need to do so that the Message Box shows all 5 Random Numbers?

Code:
 
'FILENAME:  DateTime.vbs     PROGRAMMING LANGUAGE:  Visual BASIC Script
'COMPUTER PROGRAMMING LANGUAGE PLATFORM:  Compaq Armada 1750 Windows Script Host (Windows 2000 Professional)
'AUTHOR/S:  Steven T. Querin
'CREATED ON:  3 April 2009     EDITED ON:  21 April 2009
 
' V I S U A L   B A S I C   S C R I P T   P R O G R A M M I N G   N O T E S
 
' A U T H O R ' S   N O T E S   &   E D I T I N G   N O T E S
 
' D E C L A R E D   V A R I A B L E S   &   C O N S T A N T S
Dim dtmSeed     'Randomized Seed Number based on the Date/Time Functions
Dim Number1     'numbers generated by Rnd Function
Dim Number2     'numbers generated by Rnd Function
Dim Number3     'numbers generated by Rnd Function
Dim Number4     'numbers generated by Rnd Function
Dim Number5     'numbers generated by Rnd Function
Dim Randomizer     'Variable used for calling Random Number Generator Procedures
 
' D A T E T I M E . V B S   M A I N   P R O G R A M
dtmSeed = Time
MsgBox dtmSeed & vbNewLine & "Hour = " & Hour(dtmSeed) & vbNewLine & "Minute = " & Minute(dtmSeed) & vbNewLine & _
  "Second = " & Second(dtmSeed)
 
dtmSeed = Time
Randomize[Second(dtmSeed)]
Number1 = Rnd
Number2 = Rnd
Number3 = Rnd
Number4 = Rnd
Number5 = Rnd
MsgBox dtmSeed & vbNewLine & vbNewLine & Number1 & vbNewLine & Number2 & vbNewLine & Number3 & vbNewLine & Number4 & _
  vbNewLine & Number5
 
' D A T E T I M E . V B S   P R O C E D U R E S
 
' S E T U P . V B S   P R O C E D U R E S
QUESTION #2
?How can I convert the above Code into a Single-Dimension Array?

VBScript Third Edition shows a Single-Dimension Array Example by listing 6 Colors in a Text Format. However, there is no example Single-Dimension Coding; or, any Downloaded FileNames for the Single-Dimension Array of the List Of Colors.

I have tried to use the following Code Lines; but, I get Error Messages during Runtime.

Code:
 
Dim dtmSeed
Dim Counter
Dim Number
Dim Number(Counter)
 
dtmSeed = Time
Randomize[Second(dtmSeed)]
For Counter = 1 to 5
     Number(Counter) = Rnd
Next     'Counter
MsgBox Number(1) & vbNewLine & Number(2) & vbNewLine & Number(3) & vbNewLine & Number(4) & vbNewLine & Number(5)
I would have zero problem with getting the above Code Sequences to work in GW-BASIC.

Also..."Dim Number" and "Dim Number(Counter)" were used in separate Coding Attempts; and, don't appear in the same Code together. I have the two together in the above Code Sample to reduce the amount of typing for this Posting.
Reply With Quote
  #2 (permalink)  
Old April 22nd, 2009, 05:58 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

This is *NOT* legal VBScript code, so I have no idea how your code works, at all:
Code:
Randomize[Second(dtmSeed)]
VBscript does NOT use [...] for any purpose, whatsoever.

ALSO...

VBScript does *NOT* need an argument to RANDOMIZE. If you omit the argument, you get the best possible randomization. So you SHOULD omit it.

***************

In your second set of code, this line is illegal:
Code:
Dim Number(Counter)
First of all, you didn't assign any value to Counter before trying to use it. But in any case, VBScript does *NOT* allow an expression in DIMensioning an array.

You could (and, in this case, SHOULD) have simply coded
Code:
Dim Number(5)
and the code should have worked, excepting again for those bogus square brackets with Randomize.

******************

A much better/simpler way to have coded this would have been to recognize that in VBScript *ALL* arrays start at element 0 (not 1) and so you could have done simply:
Code:
Dim Counter
Dim Number(4)
 
Randomize
For Counter = 0 To UBound(Number)
     Number(Counter) = Rnd
Next 
MsgBox Join(Number,vbNewLine)
Reply With Quote
  #3 (permalink)  
Old April 30th, 2009, 09:44 PM
Authorized User
 
Join Date: Apr 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

QUESTION #1
This still remains unanswered.

I think that I need to create a new Variable that stores the 5 "Number#" Variables then use the new Variable in the Message Box.

QUESTION #2
That explains the "Constant" Error Messages that I'm getting.

RANDOMIZE[SECOND(DTMSEED)]
Actually, the "Randomize[Second(dtmSeed)]" does work within my VBScript Codes.

VBScript Third Edition Page 615:
Syntax Randomize[Number]

Although "Randomize[Second(dtmSeed)]" does work, VBScript Third Edition does have listed Code Listings within the Book Text that looks like "Randomize Second(dtmSeed)". In fact, some of my Programs are written with the latter Randomize format.

My "dtmSeed" is a Variable that stores the Date/Time Function Strings. What this does is allow me to use a single Date/Time String for generating Random Seed Numbers throughout the Program; instead of, using the current Date/Time Strings that the Date/Time Functions call out.

Also, I am only using NotePad and the Windows 2000 Profession Windows Script Host for my VBScript Codes; thus, I don't get any IDE Errors that will come with VBScript Supported IDEs for "Randomize[Second(dtmSeed)]" syntaxes.

It is also possible that newer versions of Windows are more strict about VBScript syntax than Windows 2000 Professional.

I am not programming for Internet Applications, or for integration with other Computer Languages; thus, I probably can get away with more syntax dialects.

My Computer Programming activities are centered around Video/Personal Computer Gaming; and, the Psuedo-Random Number Generation Codes listed in VBScript Third Edition don't meet my standards for Random Number Generation.

There are times when one wants a Character to be in a Specific Location doing a Specific Activity at a Specific Time after Game Start, every time the Computer is Turned On; but, I don't want that Psuedo-Randomness in my Video/Personal Computer Games.

I am using VBScript to learn about Windows Programming. Windows Programming is so complicated as compared to DOS Programming with GW-BASIC.

Although Liberty BASIC seems to be Windows version of GW-BASIC, the Books and Compilers that I got on Liberty BASIC isn't as complete or thorough as VBScript Third Edition and Learning BASIC For TANDY Computers; and, they encompass other Computer Programming Languages as well.

Last edited by Tandy1000SL GW-BASIC; April 30th, 2009 at 10:09 PM..
Reply With Quote
  #4 (permalink)  
Old May 1st, 2009, 04:42 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

*SIGH*
Quote:
VBScript Third Edition Page 615:
Syntax Randomize[Number]
Look, that is just standard "book notation" for saying that the Number argument to Randomize is *OPTIONAL*.

But you are correct: For some oddball reason, the [...] characters do *NOT* cause an error in a ".vbs" file. BUT... They also do *NOT* allow their contents to be "seen" by the VBS code!

That is, doing
Code:
Randomize [Second(Now()]
produces the same results as doing
Code:
Randomize 0
So even though they don't cause an error, they *DO* mess up your program.

But as for only getting 4 numbers in your MSGBOX: No idea what you are talking about.

When I created a "junk.vbs" file that looked like this:
Code:
Randomize
Number1 = Rnd
Number2 = Rnd
Number3 = Rnd
Number4 = Rnd
Number5 = Rnd
MsgBox Number1 & vbNewLine & Number2 & vbNewLine & Number3 & vbNewLine & Number4 & _
  vbNewLine & Number5
it worked just fine, showing all 5 random numbers in the MsgBox.

And you can confirm what I said about the [...]. Try these to VBS scripts and compare the results you get:
Code:
Randomize [Second(Time)] ' shortened form of your code
Number1 = Rnd
Number2 = Rnd
Number3 = Rnd
Number4 = Rnd
Number5 = Rnd
MsgBox Number1 & vbNewLine & Number2 & vbNewLine & Number3 & vbNewLine & Number4 & vbNewLine & Number5
and then
Code:
 Randomize 0
 Number1 = Rnd
 Number2 = Rnd
 Number3 = Rnd
 Number4 = Rnd
 Number5 = Rnd
MsgBox Number1 & vbNewLine & Number2 & vbNewLine & Number3 & vbNewLine & Number4 & vbNewLine & Number5
SO STOP USING [...]

*******************
Quote:
There are times when one wants a Character to be in a Specific Location doing a Specific Activity at a Specific Time after Game Start, every time the Computer is Turned On; but, I don't want that Psuedo-Randomness in my Video/Personal Computer Games.
Perhaps so. But then how do you MIX this capability (easy to do by, indeed, seeding Randomize with a constant, as we just saw) with truly random behavior needed in other parts of the game?

Sounds to me like you need two pseudo-random number generators, one that you tightly control and one that you just initialize with Randomize alone. But for that, you will likely have to create your own "tightly controlled" generator. Not hard, since you don't want true randomness.

********
Quote:
It is also possible that newer versions of Windows are more strict about VBScript syntax than Windows 2000 Professional
No. If you have installed MSIE version 5 or later on that machine, then you *HAVE* gotten the latest version of VBScript. The install of MSIE includes an install of both VBScript and JScript DLLs.

**********

Not sure how VBScript is going to be really useful for video game programming. It doesn't have any graphics capability, at all. If you really want to learn how to do windows-based graphic programming, I think you'd be a *LOT* better off learning VB.NET. Among other things, your games might run at decent speeds, not at the snail slow speed of VBScript.

Oh, and by the by, I use notepad for programming VBS, too. There aren't any good editors for VBS.
Reply With Quote
  #5 (permalink)  
Old May 1st, 2009, 04:53 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Quote:
My "dtmSeed" is a Variable that stores the Date/Time Function Strings. What this does is allow me to use a single Date/Time String for generating Random Seed Numbers throughout the Program; instead of, using the current Date/Time Strings that the Date/Time Functions call out.
If you keep thinking of dtmSeed as holding a "String", you are going to make life much more difficult on yourself, in the long run.

No, dtmSeed does *NOT* hold a STRING, at all.

It holds a vbScript data type known as vbDate.

You can confirm this by doing
Code:
dtmSeed = Time( ) ' the parens aren't needed, but they *are* more "correct"

MsgBox "dtmSeed has typename " & TypeName(dtmSeed) & " and vartype " & VarType(dtmseed)
If you have not already done so, you really really really should download the MS Script documentation in the form of a "Help" file from here:
http://www.microsoft.com/downloads/d...displaylang=en
and then maybe the quick reference "card" from here:
http://www.microsoft.com/downloads/d...displaylang=en
Reply With Quote
  #6 (permalink)  
Old May 2nd, 2009, 01:28 PM
Authorized User
 
Join Date: Apr 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Old Pendant",

You need to re-read my last Posting.
You totally mis-interpreted my comments about Psuedo-Random Numbers.

You haven't even read VBScript Third Edition; thus, your comments about my VBScript Code syntax is pointless.

Obviously, your Computer Programming Skill & Technique comes from non-VBScript Computer Programming Languages; and, you are trying to apply that knowledge to VBScript syntax; which, will sometimes be incorrect; and, you are being incorrect.

VBScript and Visual BASIC are two different Programming Languages; thus, they have different syntaxes; although, they are extremely similar. This is no different to the (c)1980 DUNGEONS & DRAGONS Basic Rules and Expert Rules being very similar to; but, different from the (c)1974 ADVANCED DUNGEONS & DRAGONS Roleplaying Game System.

?vbDate?

The Data Type stored in my "dtmSeed" Variable is a String Data, not a Numeric Data, not an Array Data, not an Object Data, not a Null; and, not an Empty.

All Date/Time Data are considered as String Data.
String Data can contain Numbers; and, Letters; and, Characters like ":" & " ".

There is no confusion on my part.

All VBScript Variables are Varients; thus, any and all kinds of data can be stored in any particular VBScript Variable.

Last edited by Tandy1000SL GW-BASIC; May 2nd, 2009 at 01:48 PM..
Reply With Quote
  #7 (permalink)  
Old May 2nd, 2009, 05:16 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

This is my last reply to you.

It is pointless to continue this discussion.
Quote:
Obviously, your Computer Programming Skill & Technique comes from non-VBScript Computer Programming Languages; and, you are trying to apply that knowledge to VBScript syntax; which, will sometimes be incorrect; and, you are being incorrect.
Damn. I guess those 2 years I spent converting Microsoft's VBScript engine to work on non-MS platforms (IBM mainframe, IBM Unix, Linux) didn't teach me a single thing about the language.

Damn. I guess these past 8 (almost 9) years that I have spent posting close to 100,000 answers on various forums (probably more than 60% of them relevant to VBScript) were a waste of time.

Damn. I guess all those FAQs and articles I wrote (okay, most of them back in 2001, that's true) are total lies.


Quote:
The Data Type stored in my "dtmSeed" Variable is a String Data, not a Numeric Data, not an Array Data, not an Object Data, not a Null; and, not an Empty.

All Date/Time Data are considered as String Data.
You couldn't be more wrong if you tried.

Dates and times stored in VBScript are stored in a Variant. The variant type is vt_Date and the variant number is the same as the VarType in VBScript. To wit, vbDate, which just happens to be evaluate to the number 7. The actual data is stored as an 8 byte floating point number. The integer portion of that number is the number of days elapsed since 30 December 1899 (yes, 30th, not the 31st). The fractional part of that number is the fraction of 24 hours that represents the time. There is *CLEARLY* NO STRING involved. ONLY when you convert that value for display to a human being is it THEN converted to a string. Here, an FAQ I wrote on that way back in 2002:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=189

And I even showed you the code that would prove you wrong (using VBScript's builtin VARTYPE and TYPENAME functions). But you apparently will continue to refuse to learn from anything other than what is apparently a flawed book. Or, much more likely, your flawed interpretation of it.

By the way, the forum about the book you are using is here:
http://p2p.wrox.com/book-vbscript-programmers-reference-1st-2nd-3rd-editions-51/
There hasn't been a post in that forum since January 9th.

Last edited by Old Pedant; May 2nd, 2009 at 06:29 PM.. Reason: Typos and added link
Reply With Quote
  #8 (permalink)  
Old May 5th, 2009, 02:23 PM
Authorized User
 
Join Date: Apr 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Old Pedant",

ONE
Sorry, about misreading your UserName.

TWO
The book VBScript Third Edition does have errors in it.

However, the Code that I have written based on that book works on my Computer System.

You and I are in agreement that all VBScript Variables are Variants.

It is obvious to me that you have more knowledge and understanding about Computer Programming than I do; but, that doesn't change the fact that my VBScript Code syntax does work on my Computer System; or, that I was wrong about "Else" and "ElseIf" in another Topic Thread.

Although I am an Amateur Programmer; whereas, you are a Professional Programmer, I am aware that there are minor differences between Syntax Dialect Styles within specific Computer Programming Languages.

EXAMPLE: The authors of SAMS TEACH YOURSELF...VBSCRIPT...IN 24 HOURS will have written Codes slightly different from how the authors of VBScript Third Edition have written the same Code. Those differences doesn't make one source correct and the other wrong; but, reflect the fluency of the VBScript Computer Programming Language.

This fleuncy is no different from the fluency of all Human spoken languages.

THREE
I thought this Forum covered VBScript Third Edition. Well...I am new to these Forums; which, explains my error in judgement.
Reply With Quote
  #9 (permalink)  
Old November 1st, 2009, 11:07 PM
Authorized User
 
Join Date: Apr 2009
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Old Pedant",

You are correct about the brackets. I do need to stop using them in my Programs.

As of 1 November 2009, I have yet to test out your Sample Routines.

With GW-BASIC, I was able to create Fully-Random Number Generators very easily. I haven't been successful with VBScript to date. Again, I need to test your Example Routines.

I could easily use Psuedo-Random Number Generators in both GW-BASIC and VBScript; but, my Programming Theories require Fully-Random Number Generation.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
separate a string with whitespace petergoodman XSLT 8 July 8th, 2008 01:56 AM
Two separate menu from one sitemap? KieshaJ310 ASP.NET 2.0 Basics 5 July 4th, 2007 01:31 AM
Search separate C from CR topshed Classic ASP Databases 2 December 29th, 2005 08:58 AM
Code Download Questions Ben Horne Forum and Wrox.com Feedback 18 April 21st, 2004 09:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.