i need to generate a random, 20 character string that will only contain 0-9
and A-Z

any good solutions before i start writing something inefficient?

:)

Re: how to generate random string? by JTW

JTW
Fri Sep 15 10:08:01 CDT 2006

You can start with something like this:

Randomize Second(Now)
Do
intEval = Int((43 * Rnd) + 48)
If intEval > 47 And intEval < 58 Then
strRetVal = strRetval & Chr(intEval)
ElseIf intEval > 64 And intEval < 91 Then
strRetVal = strRetVal & Chr(intEval)
End If

Loop Until Len(strRetVal) = 20

WScript.Echo strRetVal

http://www.dx21.com/SCRIPTING/VBSCRIPT/VIEWITEM.ASP?NTI=4&SI=7&OID=44&VL
=F&TN=Intrinsic&IDF=IID

http://www.asciitable.com/

--
Jase T. Wolfe
Dx21, LLC
http://www.Dx21.com


Jason Simmons wrote:

> i need to generate a random, 20 character string that will only
> contain 0-9 and A-Z
>
> any good solutions before i start writing something inefficient?
>
> :)

Re: how to generate random string? by Jason

Jason
Fri Sep 15 10:17:38 CDT 2006

what is "Randomize Second(Now)" doing?

im used to seeing simple "Randomize" to start the random number generator?


"JTW" <Newsgroup@Dx21.com> wrote in message
news:%23uuS9iN2GHA.4976@TK2MSFTNGP02.phx.gbl...
> You can start with something like this:
>
> Randomize Second(Now)
> Do
> intEval = Int((43 * Rnd) + 48)
> If intEval > 47 And intEval < 58 Then
> strRetVal = strRetval & Chr(intEval)
> ElseIf intEval > 64 And intEval < 91 Then
> strRetVal = strRetVal & Chr(intEval)
> End If
>
> Loop Until Len(strRetVal) = 20
>
> WScript.Echo strRetVal
>
> http://www.dx21.com/SCRIPTING/VBSCRIPT/VIEWITEM.ASP?NTI=4&SI=7&OID=44&VL
> =F&TN=Intrinsic&IDF=IID
>
> http://www.asciitable.com/
>
> --
> Jase T. Wolfe
> Dx21, LLC
> http://www.Dx21.com
>
>
> Jason Simmons wrote:
>
>> i need to generate a random, 20 character string that will only
>> contain 0-9 and A-Z
>>
>> any good solutions before i start writing something inefficient?
>>
>> :)



Re: how to generate random string? by JTW

JTW
Fri Sep 15 10:23:44 CDT 2006

Its an optional seed number for the Randomize routine. If not
provided, the system timer is used. I like providing a number -
perhaps "just because".

http://tinyurl.com/jdgg7

--
Jase T. Wolfe
Dx21, LLC
http://www.Dx21.com


Jason Simmons wrote:

> what is "Randomize Second(Now)" doing?
>
> im used to seeing simple "Randomize" to start the random number
> generator?
>
>
> "JTW" <Newsgroup@Dx21.com> wrote in message
> news:%23uuS9iN2GHA.4976@TK2MSFTNGP02.phx.gbl...
> > You can start with something like this:
> >
> > Randomize Second(Now)
> > Do
> > intEval = Int((43 * Rnd) + 48)
> >If intEval > 47 And intEval < 58 Then
> > strRetVal = strRetval & Chr(intEval)
> >ElseIf intEval > 64 And intEval < 91 Then
> > strRetVal = strRetVal & Chr(intEval)
> > End If
> >
> > Loop Until Len(strRetVal) = 20
> >
> > WScript.Echo strRetVal
> >
> > http://www.dx21.com/SCRIPTING/VBSCRIPT/VIEWITEM.ASP?NTI=4&SI=7&OID=4
> > 4&VL =F&TN=Intrinsic&IDF=IID
> >
> > http://www.asciitable.com/
> >
> > -- Jase T. Wolfe
> > Dx21, LLC
> > http://www.Dx21.com
> >
> >
> > Jason Simmons wrote:
> >
> > > i need to generate a random, 20 character string that will only
> > > contain 0-9 and A-Z
> > >
> > > any good solutions before i start writing something inefficient?
> > >
> > > :)

Re: how to generate random string? by Jason

Jason
Fri Sep 15 10:36:23 CDT 2006

gotcha!

thought... Second(Now) might not be a good seed in the event you need to
call this function more than once per second... the seed will be the same.


"JTW" <Newsgroup@Dx21.com> wrote in message
news:%23GQ8vrN2GHA.3372@TK2MSFTNGP04.phx.gbl...
> Its an optional seed number for the Randomize routine. If not
> provided, the system timer is used. I like providing a number -
> perhaps "just because".
>
> http://tinyurl.com/jdgg7
>
> --
> Jase T. Wolfe
> Dx21, LLC
> http://www.Dx21.com
>
>
> Jason Simmons wrote:
>
>> what is "Randomize Second(Now)" doing?
>>
>> im used to seeing simple "Randomize" to start the random number
>> generator?
>>
>>
>> "JTW" <Newsgroup@Dx21.com> wrote in message
>> news:%23uuS9iN2GHA.4976@TK2MSFTNGP02.phx.gbl...
>> > You can start with something like this:
>> >
>> > Randomize Second(Now)
>> > Do
>> > intEval = Int((43 * Rnd) + 48)
>> >If intEval > 47 And intEval < 58 Then
>> > strRetVal = strRetval & Chr(intEval)
>> >ElseIf intEval > 64 And intEval < 91 Then
>> > strRetVal = strRetVal & Chr(intEval)
>> > End If
>> >
>> > Loop Until Len(strRetVal) = 20
>> >
>> > WScript.Echo strRetVal
>> >
>> > http://www.dx21.com/SCRIPTING/VBSCRIPT/VIEWITEM.ASP?NTI=4&SI=7&OID=4
>> > 4&VL =F&TN=Intrinsic&IDF=IID
>> >
>> > http://www.asciitable.com/
>> >
>> > -- Jase T. Wolfe
>> > Dx21, LLC
>> > http://www.Dx21.com
>> >
>> >
>> > Jason Simmons wrote:
>> >
>> > > i need to generate a random, 20 character string that will only
>> > > contain 0-9 and A-Z
>> > >
>> > > any good solutions before i start writing something inefficient?
>> > >
>> > > :)



Re: how to generate random string? by Justin

Justin
Fri Sep 15 10:48:10 CDT 2006

On Fri, 15 Sep 2006 10:36:23 -0500, Jason Simmons <js@js.com> wrote:

> gotcha!
>
> thought... Second(Now) might not be a good seed in the event you need to
> call this function more than once per second... the seed will be the
> same.
>

It's a wretchedly bad seed under any circumstances, since it limits you to
only sixty distinct sets of random numbers.

--
Justin Piper
Bizco Technologies
http://www.bizco.com/

Re: how to generate random string? by Walter

Walter
Fri Sep 15 11:22:26 CDT 2006


"Jason Simmons" <js@js.com> wrote in message
news:%23UDNWXN2GHA.476@TK2MSFTNGP06.phx.gbl...
:i need to generate a random, 20 character string that will only contain 0-9
: and A-Z
:
: any good solutions before i start writing something inefficient?
:
::)
:
:

Const Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
RandomString = ""
Randomize
For Count = 1 To 20
RandomString = RandomString & Mid(Chars,Int(36 * Rnd + 1),1)
Next



Re: how to generate random string? by Jason

Jason
Fri Sep 15 13:00:22 CDT 2006

much more elegant, thank you


"Walter Zackery" <please_respond_to@group.com> wrote in message
news:%23w8ziMO2GHA.4176@TK2MSFTNGP06.phx.gbl...
>
> "Jason Simmons" <js@js.com> wrote in message
> news:%23UDNWXN2GHA.476@TK2MSFTNGP06.phx.gbl...
> :i need to generate a random, 20 character string that will only contain
> 0-9
> : and A-Z
> :
> : any good solutions before i start writing something inefficient?
> :
> ::)
> :
> :
>
> Const Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
> RandomString = ""
> Randomize
> For Count = 1 To 20
> RandomString = RandomString & Mid(Chars,Int(36 * Rnd + 1),1)
> Next
>
>



Re: how to generate random string? by Dr

Dr
Sat Sep 16 12:02:37 CDT 2006

JRS: In article <op.tfxfhbebcs3d1w@luxembourg.psg.bizcotech.com>, dated
Fri, 15 Sep 2006 15:48:10 remote, seen in news:microsoft.public.scriptin
g.vbscript, Justin Piper <jpiper@bizco.com> posted :
>On Fri, 15 Sep 2006 10:36:23 -0500, Jason Simmons <js@js.com> wrote:
>
>> gotcha!
>>
>> thought... Second(Now) might not be a good seed in the event you need to
>> call this function more than once per second... the seed will be the
>> same.
>>
>
>It's a wretchedly bad seed under any circumstances, since it limits you to
>only sixty distinct sets of random numbers.

And even worse if the program is run by a scheduler.

I'd hope that, without parameters, Randomize uses either a combination
of the date and time, and/or possibly the time since boot. If it
matters, check it.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines

Re: how to generate random string? by Richard

Richard
Sat Sep 16 22:25:11 CDT 2006


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:aqia2KFt4CDFFwWD@merlyn.demon.co.uk...
> I'd hope that, without parameters, Randomize uses either a combination
> of the date and time, and/or possibly the time since boot. If it
> matters, check it.

Documentation says that Randomize seeds the Rnd function with a value from
the Timer function. The Timer function returns the number of seconds since
midnight, to the nearest 100th of a second. The values range from 0.00 to
86400.00, so 8,640,000 values.

Rnd itself is said to be a 24-bit Linear Congruential Generator of maximal
length, so it can generate 16,777,216 (2^24) different values. However, only
7 digits are displayed.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



Re: how to generate random string? by Dr

Dr
Mon Sep 18 16:34:29 CDT 2006

JRS: In article <ukNHkjg2GHA.1040@TK2MSFTNGP06.phx.gbl>, dated Sat, 16
Sep 2006 22:25:11 remote, seen in news:microsoft.public.scripting.vbscri
pt, Richard Mueller <rlmueller-NOSPAM@ameritech.NOSPAM.net> posted :
>
>"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
>news:aqia2KFt4CDFFwWD@merlyn.demon.co.uk...
>> I'd hope that, without parameters, Randomize uses either a combination
>> of the date and time, and/or possibly the time since boot. If it
>> matters, check it.
>
>Documentation says that Randomize seeds the Rnd function with a value from
>the Timer function. The Timer function returns the number of seconds since
>midnight, to the nearest 100th of a second. The values range from 0.00 to
>86400.00, so 8,640,000 values.

You mean 86399.99, 8,639,999.

No, the Timer returns the number of seconds, reported as a multiple of
0.01 seconds. It does not necessarily change every 0.01 seconds; in my
system it updates at 18.2 Hz, so takes steps of 5 or 6 in the second
decimal place. In other systems it does update faster.


>Rnd itself is said to be a 24-bit Linear Congruential Generator of maximal
>length, so it can generate 16,777,216 (2^24) different values. However, only
>7 digits are displayed.

Rnd surely generates an IEEE Double, capable of 53 bit resolution but in
this case presumably using only 24. It is conversion-to-string which
gives values with 7 or fewer decimal places or in E-format. I see, for
example,
0.7747401
1.401764E-02
0.7607236
0.81449
0.7090379

Try
document.write "<pre>"
for K = 1 to 50
R = Rnd
document.write R, " ", R*16777216, "<br>"
next
document.write "</pre>"
to see that the results of Rnd do effectively have 24 significant bits.


To be sure of getting equi-probable digits, one must either use Rnd as a
number in 0..1 or, if going via string, think rather carefully.


Also disappointing, since it has for many years been very practical to
use a generator with 32 bits. ISTR that Turbo Pascal had such in the
late 1980s.

If the application needs good Randoms, one can use javascript, or write
one's own - follow Knuth or my pas-rand.htm

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Re: how to generate random string? by Richard

Richard
Mon Sep 18 22:18:11 CDT 2006


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:3YwxaSAlDxDFFwAg@merlyn.demon.co.uk...
> JRS: In article <ukNHkjg2GHA.1040@TK2MSFTNGP06.phx.gbl>, dated Sat, 16
> Sep 2006 22:25:11 remote, seen in news:microsoft.public.scripting.vbscri
> pt, Richard Mueller <rlmueller-NOSPAM@ameritech.NOSPAM.net> posted :

...
>>
>>Documentation says that Randomize seeds the Rnd function with a value from
>>the Timer function. The Timer function returns the number of seconds since
>>midnight, to the nearest 100th of a second. The values range from 0.00 to
>>86400.00, so 8,640,000 values.
>
> You mean 86399.99, 8,639,999.
>

Yes.

> No, the Timer returns the number of seconds, reported as a multiple of
> 0.01 seconds. It does not necessarily change every 0.01 seconds; in my
> system it updates at 18.2 Hz, so takes steps of 5 or 6 in the second
> decimal place. In other systems it does update faster.
>

I expected the same, since the system timer ticks 18.2065 times per second.
Before I posted I tested on a Pentium 3 system and found that the output
incremented in 0.01 seconds. However, the OS was w2k. I also tested on an XP
system. After your posting I tested again on an identical Pentium 3, but
with Win98, and Timer incremented in larger intervals. Same on a Pentium 4
with Win98. Interesting.

>
>>Rnd itself is said to be a 24-bit Linear Congruential Generator of maximal
>>length, so it can generate 16,777,216 (2^24) different values. However,
>>only
>>7 digits are displayed.
>
> Rnd surely generates an IEEE Double, capable of 53 bit resolution but in
> this case presumably using only 24. It is conversion-to-string which
> gives values with 7 or fewer decimal places or in E-format. I see, for
> example,
> 0.7747401
> 1.401764E-02
> 0.7607236
> 0.81449
> 0.7090379
>
> Try
> document.write "<pre>"
> for K = 1 to 50
> R = Rnd
> document.write R, " ", R*16777216, "<br>"
> next
> document.write "</pre>"
> to see that the results of Rnd do effectively have 24 significant bits.
>

I find that Rnd * 16777216 is always an exact integer.

>
> To be sure of getting equi-probable digits, one must either use Rnd as a
> number in 0..1 or, if going via string, think rather carefully.
>
>
> Also disappointing, since it has for many years been very practical to
> use a generator with 32 bits. ISTR that Turbo Pascal had such in the
> late 1980s.
>
> If the application needs good Randoms, one can use javascript, or write
> one's own - follow Knuth or my pas-rand.htm

It seems that VB and VBScript are limited to 15 decimal digits. I think that
is why they limit the LCG to 24 bits. I agree this is disappointing. In
assembly it is straightforward to create a 64-bit LCG, even using 32-bit
registers. Years ago I created a psuedo random number generator in assembly
with a period of about 2^222 (in theory at least, based on Knuth).

I find the Rnd function is equivalent to the following function, divided by
16777216 and rounded to 7 digits. In the snippet below I do not round the
output of my function to 7 digits:
=============
' Constants for Linear Congruential Generator.
Const A = 16598013
Const C = 12820163
Const M = 16777216

' Initialize with any seed.
' x is series created by Rnd.
' y is series created by Function LCG.
Randomize(.5)
x = Rnd
y = x * M

' Output next 50 numbers from each.
For = 1 To 50
x = Rnd
y = LCG(y)
Wscript.Echo x & ", " y / M
Next

Function LCG(lngSeed)
LCG = A * lngSeed
LCG = LCG + C
LCG = LCG / M
LCG = (LCG - Fix(LCG)) * M
End Function
=========
My function LCG must use the integers or it won't work. Rounding to 7 digits
after dividing by M is not easy. For example, 0.008614659 is not rounded to
0.0086147.

I have not figured out what the Randomize function does. It accepts any
number you give it. I have not been able to find a value to pass to
Randomize that duplicates what it does when you do not pass a value to it.
For example, Randomize(Timer) is not equivalent to Randomize().

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



Re: how to generate random string? by Dr

Dr
Wed Sep 20 12:11:11 CDT 2006

JRS: In article <eYR09o52GHA.1060@TK2MSFTNGP04.phx.gbl>, dated Mon, 18
Sep 2006 22:18:11 remote, seen in news:microsoft.public.scripting.vbscri
pt, Richard Mueller <rlmueller-NOSPAM@ameritech.NOSPAM.net> posted :
> ...
>It seems that VB and VBScript are limited to 15 decimal digits.

Like javascript, VB & VBS use IEEE Doubles, and can resolve every
integer up to 2^53.

> I think that
>is why they limit the LCG to 24 bits. I agree this is disappointing.

IE4 Javascript seems to use at least about 53 bits.


>I find the Rnd function is equivalent to the following function, divided by
>16777216 and rounded to 7 digits.

No, Rnd will not round to 7 digits, but to 53 significant bits. It's
the I/O that gives up to 7 digits.

> In the snippet below I do not round the
>output of my function to 7 digits:
>=============
>' Constants for Linear Congruential Generator.
>Const A = 16598013
>Const C = 12820163
>Const M = 16777216

From that, it is AIUI possible to derive the corresponding constants for
the reverse sequence. I wish I knew how.

Probably Rnd updates its stored value then turns it into a Float, rather
than doing it in the opposite order. If the float conversion is
straightforward (and I see no sense in it not being) then one could
tentatively deduce what Randomize produces, given the first Rnd.

> ...

>For example, Randomize(Timer) is not equivalent to Randomize().

I suspect Timer gets H M S cs from a DOS call and does the obvious
multiply/add arithmetic. IIRC, Borland Pascal's Randomize takes the
four byte-size fields in some order as the bytes of the initial seed.
Maybe VB Randomize does similarly; it's more efficient.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.