# Convert Vb code to VBscript

K

#### kaymaf

Hi,
Can someone help me convert the code below to vbscript or any idea how to
generate random number as string (at least 16 digits).
please any ideas or hints
=========================
Public Function generateSerialNumber_25DIGITS(ByVal NumberOfSerial As
Integer) As ArrayList
Dim total As Integer = 0
Dim serialList As New ArrayList
Dim rn As New Random
Dim gn, gn2, gn3, gn4, gn5 As Integer
While total <= NumberOfSerial - 1
gn = rn.Next(10000, 99999)
gn2 = rn.Next(10000, 99999)
gn3 = rn.Next(10000, 99999)
gn4 = rn.Next(10000, 99999)
gn5 = rn.Next(10000, 99999)
If gn Mod 4 = 1 And gn2 Mod 4 = 0 And gn3 Mod 4 = 1 And gn4 Mod
4 = 0 And gn5 Mod 4 = 1 Then
serialList.Add(gn & "-" & gn2 & "-" & gn3 & "-" & gn4 & "-"
& gn5)
total = total + 1
End If
End While
Return serialList
End Function

=========================
thanks
kaymaf
--
Never stop learning

## System One

P

#### Pegasus [MVP]

This line will create a random number of 16 digits:
iRandom = Left(Int(Rnd * 1000000000000000) & Int(Rnd * 1000000000000000),
16)

"kaymaf" <kaymaf@newsgroup> wrote in message
news:35D5C1C2-B163-4439-82E5-7591B1BB0662@newsgroup

> Hi,
> Can someone help me convert the code below to vbscript or any idea how to
> generate random number as string (at least 16 digits).
> please any ideas or hints
> =========================
> Public Function generateSerialNumber_25DIGITS(ByVal NumberOfSerial As
> Integer) As ArrayList
> Dim total As Integer = 0
> Dim serialList As New ArrayList
> Dim rn As New Random
> Dim gn, gn2, gn3, gn4, gn5 As Integer
> While total <= NumberOfSerial - 1
> gn = rn.Next(10000, 99999)
> gn2 = rn.Next(10000, 99999)
> gn3 = rn.Next(10000, 99999)
> gn4 = rn.Next(10000, 99999)
> gn5 = rn.Next(10000, 99999)
> If gn Mod 4 = 1 And gn2 Mod 4 = 0 And gn3 Mod 4 = 1 And gn4 Mod
> 4 = 0 And gn5 Mod 4 = 1 Then
> serialList.Add(gn & "-" & gn2 & "-" & gn3 & "-" & gn4 & "-"
> & gn5)
> total = total + 1
> End If
> End While
> Return serialList
> End Function
>
> =========================
> thanks
> kaymaf
> --
> Never stop learning

## System One

R

#### Richard Mueller [MVP]

If you don't want the value to be the same every time, initialize the Rnd
function with the statement:

Randomize

before using the Rnd function.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"Pegasus [MVP]" <news@newsgroup> wrote in message
news:%23Q5fplEOKHA.5084@newsgroup

> This line will create a random number of 16 digits:
> iRandom = Left(Int(Rnd * 1000000000000000) & Int(Rnd * 1000000000000000),
> 16)
>
> "kaymaf" <kaymaf@newsgroup> wrote in message
> news:35D5C1C2-B163-4439-82E5-7591B1BB0662@newsgroup

>> Hi,
>> Can someone help me convert the code below to vbscript or any idea how
>> to
>> generate random number as string (at least 16 digits).
>> please any ideas or hints
>> =========================
>> Public Function generateSerialNumber_25DIGITS(ByVal NumberOfSerial As
>> Integer) As ArrayList
>> Dim total As Integer = 0
>> Dim serialList As New ArrayList
>> Dim rn As New Random
>> Dim gn, gn2, gn3, gn4, gn5 As Integer
>> While total <= NumberOfSerial - 1
>> gn = rn.Next(10000, 99999)
>> gn2 = rn.Next(10000, 99999)
>> gn3 = rn.Next(10000, 99999)
>> gn4 = rn.Next(10000, 99999)
>> gn5 = rn.Next(10000, 99999)
>> If gn Mod 4 = 1 And gn2 Mod 4 = 0 And gn3 Mod 4 = 1 And gn4
>> Mod
>> 4 = 0 And gn5 Mod 4 = 1 Then
>> serialList.Add(gn & "-" & gn2 & "-" & gn3 & "-" & gn4 &
>> "-"
>> & gn5)
>> total = total + 1
>> End If
>> End While
>> Return serialList
>> End Function
>>
>> =========================
>> thanks
>> kaymaf
>> --
>> Never stop learning
>
>

## System One

K

#### kaymaf

Thanks Pegasus and Mueller
I didnt know that Vbscript supports .NET, so i use random class from .net
Set rn = CreateObject("System.Random")

thanks
kaymaf

## System One

D

#### Dr J R Stockton

In microsoft.public.scripting.vbscript message <35D5C1C2-B163-4439-82E5-
7591B1BB0662@newsgroup>, Thu, 17 Sep 2009 18:49:01, kaymaf
<kaymaf@newsgroup> posted:

> Can someone help me convert the code below to vbscript or any idea how to
>generate random number as string (at least 16 digits).
Be aware that the random number generator provided may well not have
enough internal states to sustain a fully-random choice of 16 decimal
digits.

See <URL:http://www.merlyn.demon.co.uk/vb-maths.htm#Rand>, which
demonstrates that the results of Rnd are given by
X[n+1] = ( X[n] * 16598013 + 12820163 ) mod 16777216
followed by division by 16777216 = 2^24.

That gives mediocre performance.

If quality matters, look for VBScript KISS, after having read
<URL:http://www.merlyn.demon.co.uk/js-randm.htm#Rand>.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

## System One

A

#### Al Dunbar

"Dr J R Stockton" <reply0938@newsgroup> wrote in message
news:GXaxBoHiyRtKFwrM@newsgroup

> In microsoft.public.scripting.vbscript message <35D5C1C2-B163-4439-82E5-
> 7591B1BB0662@newsgroup>, Thu, 17 Sep 2009 18:49:01, kaymaf
> <kaymaf@newsgroup> posted:
>

>> Can someone help me convert the code below to vbscript or any idea how
>> to
>>generate random number as string (at least 16 digits).
It seems to me that what is being asked for is more like a random sequence
of at least 16 digits than an actual "random number". If that is the case,
then a better approach would may

> Be aware that the random number generator provided may well not have
> enough internal states to sustain a fully-random choice of 16 decimal
> digits.
>
> See <URL:http://www.merlyn.demon.co.uk/vb-maths.htm#Rand>, which
> demonstrates that the results of Rnd are given by
> X[n+1] = ( X[n] * 16598013 + 12820163 ) mod 16777216
> followed by division by 16777216 = 2^24.
>
> That gives mediocre performance.
>
> If quality matters, look for VBScript KISS, after having read
> <URL:http://www.merlyn.demon.co.uk/js-randm.htm#Rand>.
>
> --
> (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9
> Sf3
> news:comp.lang.javascript FAQ
> <URL:http://www.jibbering.com/faq/index.html>.
> <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
> sources.
> <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,

## System One

A

#### Al Dunbar

"Dr J R Stockton" <reply0938@newsgroup> wrote in message
news:GXaxBoHiyRtKFwrM@newsgroup

> In microsoft.public.scripting.vbscript message <35D5C1C2-B163-4439-82E5-
> 7591B1BB0662@newsgroup>, Thu, 17 Sep 2009 18:49:01, kaymaf
> <kaymaf@newsgroup> posted:
>

>> Can someone help me convert the code below to vbscript or any idea how
>> to
>>generate random number as string (at least 16 digits).
I'm not sure what the OP had in mind by suggesting that the number be "at
least" 16 digits in length. You can almost safely assume that asking a
person to pick a number between 1 and 10 will result in as random a number
as humanly possible. Asking for one with at least 16 digits would be like
asking a person to pick a number with at least two digits (which would, in
turn, be like asking him to "pick a number between 10"). Since the highest
allowable number is not known, the results of a large number of samples from
a variety of volunteers would undoubtedly display a distribution across the
solution space that is definitely not uniform.

Another issue is that even 16-digit numbers with truly random "values" may
not necessarily exhibit digit "sequences" that appear truly random.
Certainly, if enough samples are taken, then the relative frequency of
numbers with non-random digit sequences (such as 1111111111111111,
2222222222222222, 3434343434343434, 1234567812345678, etc) would likely be
so low that one would have difficulty in accepting them as "random".

It seems to me that what is being asked for is more like a sequence of 16
random digits than an actual "random number". If that is the case, then a
better approach may be to use RND in a loop to select each digit in some
random fashion, and concatenate them into the resultant string.

I suspect that it might be difficult to determine which of two 16-digit
number sequences was generated by this method versus the "numeric" approach,
but it should be clear that the nature of the sequences is completely
different. It could possibly by dangerous, for example, to use the numeric
values of randomly selected digits in a situation where a sequence of random
numbers is actually a requirement.

But perhaps we are putting the cart before the horse, unless we consider
what the OP meant by "a random number". This term has been used to mean any
of the following:

- the least likely number that one might imagine
- a number not dependent on anything else
- a number selected completely at random (circular definitions are
wonderful)
- a number that looks random (i.e. numbers like 7777 are considered by
some as not being random, even though when expressed in hexadecimal no such
pattern would be present: 1E61. Then there are "random-looking" numbers like
65535 which, when displayed in hex (FFFF) have that non-random-appearing
pattern).

And the term "a sequence of random numbers" has been understood as a
sequence of numbers:

- in which none are repeated
- in which repeats are only allowed after all possible numbers have been
generated
- which, if repeats are allowed, they will never be successive (i.e.
this would not be allowed: 1 8 3 5 5 6 2)
- in which no patterns are discernible (by whom, I wonder)
- in which no sequences ever repeat.

> Be aware that the random number generator provided may well not have
> enough internal states to sustain a fully-random choice of 16 decimal
> digits.
I agree that the precision issue is a definite concern.

But even in those cases where the precision is not an issue, the result is
still not necessarily "fully random". RND is not a "random number generator"
but a "pseudo random number generator". See

<URL"http://en.wikipedia.org/wiki/Random_number_generation#.22True.22_random_numbers_vs._pseudorandom_numbers>.

The really cool thing about pseudo-random numbers is that, although not
"truly random", they are extremely useful in approximating true randomness
when dealing with phenomena that are not deterministic in nature.

/Al

> See <URL:http://www.merlyn.demon.co.uk/vb-maths.htm#Rand>, which
> demonstrates that the results of Rnd are given by
> X[n+1] = ( X[n] * 16598013 + 12820163 ) mod 16777216
> followed by division by 16777216 = 2^24.
>
> That gives mediocre performance.
>
> If quality matters, look for VBScript KISS, after having read
> <URL:http://www.merlyn.demon.co.uk/js-randm.htm#Rand>.
>
> --
> (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9
> Sf3
> news:comp.lang.javascript FAQ
> <URL:http://www.jibbering.com/faq/index.html>.
> <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
> sources.
> <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,

## System One

D

#### Dr J R Stockton

In microsoft.public.scripting.vbscript message <exUswzkOKHA.1232@newsgroup
NGP05.phx.gbl>, Sun, 20 Sep 2009 17:49:07, Al Dunbar
<alandrub@newsgroup> posted:

>"Dr J R Stockton" <reply0938@newsgroup> wrote in message
>news:GXaxBoHiyRtKFwrM@newsgroup

>> In microsoft.public.scripting.vbscript message <35D5C1C2-B163-4439-82E5-
>> 7591B1BB0662@newsgroup>, Thu, 17 Sep 2009 18:49:01, kaymaf
>> <kaymaf@newsgroup> posted:
>>

>>> Can someone help me convert the code below to vbscript or any idea
>>>how to
>>>generate random number as string (at least 16 digits).
>
>I'm not sure what the OP had in mind by suggesting that the number be
>"at least" 16 digits in length. You can almost safely assume that
>asking a person to pick a number between 1 and 10 will result in as
>random a number as humanly possible.
Not safe. Humans are not good at Random. Additionally, "between 1 and
10" means "2 3 4 5 6 7 8 9" but some will consider "1 2 3 4 5 6 7 8 9
10" to be allowed.

>It seems to me that what is being asked for is more like a sequence of
>16 random digits than an actual "random number".
Since the OP wrote "how to generate random number as string (at least 16
digits).", I expect that you are right. Moreover, a VBS number barely
satisfies 16 random digits, being an IEEE Double.

>> Be aware that the random number generator provided may well not have
>> enough internal states to sustain a fully-random choice of 16 decimal
>> digits.
>
>I agree that the precision issue is a definite concern.
>
>But even in those cases where the precision is not an issue, the result
>is still not necessarily "fully random". RND is not a "random number
>generator" but a "pseudo random number generator".
Indeed; and in my previous I gave its algorithm. Not only is it merely
24-bit, but /cognoscenti/ consider it low-grade.

>The really cool thing about pseudo-random numbers is that, although not
>"truly random",

That, as such, does not matter, since there are PRNGs of sufficient
cycle length that a sequence of numbers derived from them (for an
unknown PRNG & derivation) will be indistinguishable from true random.
But the PRNG used in VBS Rnd has a cycle length of only 2^24.

I recall an application where the PRNG cycle length was about 4e8 m.

The OP's code rejects all but one in 1024 of the possible results
(grossly inefficient, and trivial to improve) and generates
#####-#####-#####-#####-#####. If that's used as a sort of general-
purpose GUID, it will be decidedly unsafe, with only 2^24 possibilities.

Unfortunately, with limited testing, it may well appear adequate.

--
(c) John Stockton, near London. *@merlyn.demon.co.uk/?.?.Stockton@newsgroup
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

## System One

A

#### Al Dunbar

"Dr J R Stockton" <reply0939@newsgroup> wrote in message
news:QhzUceiH5+tKFw7t@newsgroup

> In microsoft.public.scripting.vbscript message <exUswzkOKHA.1232@newsgroup
> NGP05.phx.gbl>, Sun, 20 Sep 2009 17:49:07, Al Dunbar
> <alandrub@newsgroup> posted:

>>"Dr J R Stockton" <reply0938@newsgroup> wrote in message
>>news:GXaxBoHiyRtKFwrM@newsgroup

>>> In microsoft.public.scripting.vbscript message <35D5C1C2-B163-4439-82E5-
>>> 7591B1BB0662@newsgroup>, Thu, 17 Sep 2009 18:49:01, kaymaf
>>> <kaymaf@newsgroup> posted:
>>>
>>>> Can someone help me convert the code below to vbscript or any idea
>>>>how to
>>>>generate random number as string (at least 16 digits).
>>
>>I'm not sure what the OP had in mind by suggesting that the number be
>>"at least" 16 digits in length. You can almost safely assume that
>>asking a person to pick a number between 1 and 10 will result in as
>>random a number as humanly possible.
>
> Not safe. Humans are not good at Random. Additionally, "between 1 and
> 10" means "2 3 4 5 6 7 8 9" but some will consider "1 2 3 4 5 6 7 8 9
> 10" to be allowed.
I disagree. Given that humans are not good at Random, I still believe that
asking a person to "pick a number at random" from some agreed-upon range
will yield a number that is not as random as possible - just as random as
"humanly" possible. The only proviso would be that if you knew that the
askee's favourite number was, say, 8, then there might be a bit more
predictability in the result. But, if you knew nothing about the person,
their particular numerical biases would likely not be detectable when the
sample is limited to a single number.

Given the small sample size, you should be able to come up with a
"randomness score value" for each possible answer. Unfortunately, the number
having the highest score would become the most likely answer, and therefore
quite possibly the least random.

Of course, I wouldn't ask for a second number, as that is where our lack of
randomness becomes really apparent.

>>The really cool thing about pseudo-random numbers is that, although not
>>"truly random",
>
>
> That, as such, does not matter, since there are PRNGs of sufficient
> cycle length that a sequence of numbers derived from them (for an
> unknown PRNG & derivation) will be indistinguishable from true random.
The rub here is that I am not sure any of us is actually capable of
accurately identifying a truly random sequence as such when it presents
itself. I looked briefly, and could not find a definition of truly random.
One source suggested that basing the algorithm on the measurement of an
unpredictable white noise signal would be "random" because it would be
unpredictable. Unfortunately, the measurement method itself could have some
sort of an impact. Also, the "randomness" of the noise could be somewhat
different in nature from the randomness exhibited by the phenomenon being
studied.

> But the PRNG used in VBS Rnd has a cycle length of only 2^24.
>
> I recall an application where the PRNG cycle length was about 4e8 m.
>
> The OP's code rejects all but one in 1024 of the possible results
> (grossly inefficient, and trivial to improve) and generates
> #####-#####-#####-#####-#####. If that's used as a sort of general-
> purpose GUID, it will be decidedly unsafe, with only 2^24 possibilities.
>
> Unfortunately, with limited testing, it may well appear adequate.
I was once asked by a phd candidate for assistance with some analysis in
which he felt that if we did not include every single possible combination,
the result would be suspect. LOL. Even when I demonstrated that the result
was not significantly different from that obtained using a more modest
sample size based on some principles I have since forgotten, he would not
budge.

/Al