Hey Mr. Wilkinson!

Results 1 to 2 of 2

Thread: Hey Mr. Wilkinson!

  1. #1
    Join Date
    Dec 1969

    Default Hey Mr. Wilkinson!

    A few days ago, you fixed up someone&#039s random password generating script. I was wondering if you could break down each section of how the randomize statement works, or point me to somewhere that I could find out. I don&#039t like to just know that something works, I like to know how.<BR><BR>Thanks<BR>Steve

  2. #2
    Join Date
    Dec 1969

    Default Randomize? Or random numbers?

    Do you mean just the word "Randomize"??? <BR><BR>That&#039s just a VBScript statement that makes sure to "seed" the random number generator with the current time (down to the millisecond). If you don&#039t do that, then you can get the same supposedly random sequence over and over.<BR><BR>In VBS, the RND or RND( ) function always returns a number that is greater than OR EQUAL to zero and LESS THAN (never equal to) one.<BR><BR>So let&#039s say you need random whole numbers from 17 to 23. There are 7 possible values, right? That is:<BR>&nbsp; &nbsp; 23 - 17 + 1<BR><BR>So you multiply <BR>&nbsp; &nbsp; 7 * RND()<BR>and that gives you a result &#062;= 0 and &#060; 7, right? (7 * 0 is 0, 7 * almost 1 is almost 7, etc.)<BR><BR>Now you take the integer (whole number) part of that result:<BR>&nbsp; &nbsp; Int( 7 * RND() )<BR>so that 3.78811 becomes 3, 0.99123 becomes 0, 6.9999 becomes 6, etc.<BR><BR>Now you add *that* onto the minimum number you wanted, 17:<BR>&nbsp; &nbsp; 17 + Int( 7 * RND() )<BR>and that will give you a whole number from 17+0 to 17+6...or from 17 to 23, as we wanted.<BR><BR>So the full blown "formula" can be made into a function:<BR><BR>&#060;%<BR>Function getRandom( low, high )<BR>&nbsp; &nbsp; getRandom = low + Int( (high-low+1) * RND() )<BR>End Function<BR>%&#062;<BR><BR>And that&#039s all there is to it!<BR><BR>Naturally, if you know you need a number between 0 and N or 1 and N, you can just code getRandom "in line":<BR><BR>Between 0 and N:<BR>&nbsp; &nbsp; getRandom = Int( RND * (N+1) )<BR><BR>Between 1 and N:<BR>&nbsp; &nbsp; getRandom = 1 + Int( RND * N )<BR><BR>Is that what you were looking for?<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts