Re: HOWTO - generate a alphanumeric string of a given length
Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(le ngth As Integer) As String
Const zero As Integer = CInt("0"c)
Const nine As Integer = CInt("9"c)
Const capA As Integer = CInt("A"c)
Const capZ As Integer = CInt("Z"c)
Dim guid As Byte() = Guid.NewGuid(). ToByteArray()
Dim seed As Integer = guid(0) + Machine.Shift.L eft(guid(1), 8) +
Machine.Shift.L eft(guid(2), 16) + Machine.Shift.L eft(guid(3), 24)
Dim r As New Random(seed)
Dim s As String = Nothing
Dim i As Integer
For i = 0 To length - 1
Dim n As Integer
Do
n = r.Next(zero, capZ)
Loop While Not(n >= zero And n <= nine Or(n >= capA And n <= capZ))
s += CChar(n)
Next i
Return s
End Function 'RandomString
End Class 'StringGenerato r
Crirus wrote:[color=blue]
> Is there a function that return some random ID like string
> alphanumeric? Like this:
> A35sDsd1dSGsH
>
> Thanks
> Crirus[/color]
Re: HOWTO - generate a alphanumeric string of a given length
In article <#5$pjpTqDHA.16 80@TK2MSFTNGP10 .phx.gbl>, Crirus wrote:[color=blue]
> Is there a function that return some random ID like string alphanumeric?
> Like this:
> A35sDsd1dSGsH
>
> Thanks
> Crirus
>
>[/color]
How long do you want it?
Dim id As String = Guid.NewGuid(). ToString().Repl ace("-", String.Empty)
Console.WriteLi ne(id)
That will produce a 32 character string of hex digits (0-9 and a-f),
that is statistically unique (basically, it is pretty much guarented
that it will never be duplicated).
Re: HOWTO - generate a alphanumeric string of a given length
What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as does
Asc("0").
What's that Machine.Shift.L eft? doesn't work here. I understand what it's
meant to do though.(number between 0 and &HFFFFFFFF).
I like that Random class, hadn't seen that before, but, according to the
documentation, for best performance Random should only be created once.
StringBuilder.A ppend is faster than String +=.
You missed the LCase characters.
My much simpler version of that same function, with default time based
seeding of Random:
Private r As New Random
'I think 255 characters should be enough, although there's no reason that
'you can't use Integer or Short instead of Byte, and have more.
Private Function RandomString(By Val Stringlength As Byte) As String
Dim s As String = _
"0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZabcdefgh ijklmnopqrstuvw xyz"
Dim sb As New System.Text.Str ingBuilder
Do
sb.Append(s.Cha rs(r.Next(0, 61)))
Loop While sb.Length < Stringlength
Return sb.ToString
End Function
My version generated 100000 * 255character strings in 4 secs as opposed to
yours (updated to use stringbuilder and default seeding for comparison)
taking 6 secs(making the Random object a Class level Private object reduced
this to 5 secs). Without Stringbuilder yours took 23 secs and 21 secs.
Before that Random Class I used the old Rnd function and the fastest I could
get, even using Stringbuilder, was 28 secs.
So I learned something useful Today.
Thankyou
Mick Doherty
"One Handed Man" <Bombay@Duck.ne t> wrote in message
news:botppb$s58 $1@titan.btinte rnet.com...[color=blue]
> Try this - OHM
>
> Public Class StringGenerator
>
> Public Shared Function RandomString(le ngth As Integer) As String
> Const zero As Integer = CInt("0"c)
> Const nine As Integer = CInt("9"c)
> Const capA As Integer = CInt("A"c)
> Const capZ As Integer = CInt("Z"c)
>
> Dim guid As Byte() = Guid.NewGuid(). ToByteArray()
> Dim seed As Integer = guid(0) + Machine.Shift.L eft(guid(1), 8) +
> Machine.Shift.L eft(guid(2), 16) + Machine.Shift.L eft(guid(3), 24)
>
> Dim r As New Random(seed)
>
> Dim s As String = Nothing
> Dim i As Integer
> For i = 0 To length - 1
> Dim n As Integer
> Do
> n = r.Next(zero, capZ)
> Loop While Not(n >= zero And n <= nine Or(n >= capA And n <=[/color]
capZ))[color=blue]
> s += CChar(n)
> Next i
>
> Return s
> End Function 'RandomString
> End Class 'StringGenerato r
>
>
>
>
> Crirus wrote:[color=green]
> > Is there a function that return some random ID like string
> > alphanumeric? Like this:
> > A35sDsd1dSGsH
> >
> > Thanks
> > Crirus[/color]
>
>[/color]
Re: HOWTO - generate a alphanumeric string of a given length
What version of .NET framework are you using
Mick Doherty wrote:[color=blue]
> What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as
> does Asc("0").
> What's that Machine.Shift.L eft? doesn't work here. I understand what
> it's meant to do though.(number between 0 and &HFFFFFFFF).
> I like that Random class, hadn't seen that before, but, according to
> the documentation, for best performance Random should only be created
> once. StringBuilder.A ppend is faster than String +=.
> You missed the LCase characters.
>
> My much simpler version of that same function, with default time based
> seeding of Random:
>
> Private r As New Random
> 'I think 255 characters should be enough, although there's no reason
> that 'you can't use Integer or Short instead of Byte, and have more.
> Private Function RandomString(By Val Stringlength As Byte) As String
>
> Dim s As String = _
> "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZabcdefgh ijklmnopqrstuvw xyz"
> Dim sb As New System.Text.Str ingBuilder
>
> Do
> sb.Append(s.Cha rs(r.Next(0, 61)))
> Loop While sb.Length < Stringlength
>
> Return sb.ToString
>
> End Function
>
> My version generated 100000 * 255character strings in 4 secs as
> opposed to yours (updated to use stringbuilder and default seeding
> for comparison) taking 6 secs(making the Random object a Class level
> Private object reduced this to 5 secs). Without Stringbuilder yours
> took 23 secs and 21 secs.
>
> Before that Random Class I used the old Rnd function and the fastest
> I could get, even using Stringbuilder, was 28 secs.
>
> So I learned something useful Today.
> Thankyou
>
> Mick Doherty
>
> "One Handed Man" <Bombay@Duck.ne t> wrote in message
> news:botppb$s58 $1@titan.btinte rnet.com...[color=green]
>> Try this - OHM
>>
>> Public Class StringGenerator
>>
>> Public Shared Function RandomString(le ngth As Integer) As String
>> Const zero As Integer = CInt("0"c)
>> Const nine As Integer = CInt("9"c)
>> Const capA As Integer = CInt("A"c)
>> Const capZ As Integer = CInt("Z"c)
>>
>> Dim guid As Byte() = Guid.NewGuid(). ToByteArray()
>> Dim seed As Integer = guid(0) + Machine.Shift.L eft(guid(1), 8)
>> + Machine.Shift.L eft(guid(2), 16) + Machine.Shift.L eft(guid(3), 24)
>>
>> Dim r As New Random(seed)
>>
>> Dim s As String = Nothing
>> Dim i As Integer
>> For i = 0 To length - 1
>> Dim n As Integer
>> Do
>> n = r.Next(zero, capZ)
>> Loop While Not(n >= zero And n <= nine Or(n >= capA And n
>> <= capZ)) s += CChar(n)
>> Next i
>>
>> Return s
>> End Function 'RandomString
>> End Class 'StringGenerato r
>>
>>
>>
>>
>> Crirus wrote:[color=darkred]
>>> Is there a function that return some random ID like string
>>> alphanumeric? Like this:
>>> A35sDsd1dSGsH
>>>
>>> Thanks
>>> Crirus[/color][/color][/color]
Re: HOWTO - generate a alphanumeric string of a given length
1.1.4322.573 (VS2003)
"One Handed Man" <Bombay@Duck.ne t> wrote in message
news:bovvlj$2j1 $1@titan.btinte rnet.com...[color=blue]
> What version of .NET framework are you using
>
>
>
>
> Mick Doherty wrote:[color=green]
> > What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as
> > does Asc("0").
> > What's that Machine.Shift.L eft? doesn't work here. I understand what
> > it's meant to do though.(number between 0 and &HFFFFFFFF).
> > I like that Random class, hadn't seen that before, but, according to
> > the documentation, for best performance Random should only be created
> > once. StringBuilder.A ppend is faster than String +=.
> > You missed the LCase characters.
> >
> > My much simpler version of that same function, with default time based
> > seeding of Random:
> >
> > Private r As New Random
> > 'I think 255 characters should be enough, although there's no reason
> > that 'you can't use Integer or Short instead of Byte, and have more.
> > Private Function RandomString(By Val Stringlength As Byte) As String
> >
> > Dim s As String = _
> > "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZabcdefgh ijklmnopqrstuvw xyz"
> > Dim sb As New System.Text.Str ingBuilder
> >
> > Do
> > sb.Append(s.Cha rs(r.Next(0, 61)))
> > Loop While sb.Length < Stringlength
> >
> > Return sb.ToString
> >
> > End Function
> >
> > My version generated 100000 * 255character strings in 4 secs as
> > opposed to yours (updated to use stringbuilder and default seeding
> > for comparison) taking 6 secs(making the Random object a Class level
> > Private object reduced this to 5 secs). Without Stringbuilder yours
> > took 23 secs and 21 secs.
> >
> > Before that Random Class I used the old Rnd function and the fastest
> > I could get, even using Stringbuilder, was 28 secs.
> >
> > So I learned something useful Today.
> > Thankyou
> >
> > Mick Doherty
> >
> > "One Handed Man" <Bombay@Duck.ne t> wrote in message
> > news:botppb$s58 $1@titan.btinte rnet.com...[color=darkred]
> >> Try this - OHM
> >>
> >> Public Class StringGenerator
> >>
> >> Public Shared Function RandomString(le ngth As Integer) As String
> >> Const zero As Integer = CInt("0"c)
> >> Const nine As Integer = CInt("9"c)
> >> Const capA As Integer = CInt("A"c)
> >> Const capZ As Integer = CInt("Z"c)
> >>
> >> Dim guid As Byte() = Guid.NewGuid(). ToByteArray()
> >> Dim seed As Integer = guid(0) + Machine.Shift.L eft(guid(1), 8)
> >> + Machine.Shift.L eft(guid(2), 16) + Machine.Shift.L eft(guid(3), 24)
> >>
> >> Dim r As New Random(seed)
> >>
> >> Dim s As String = Nothing
> >> Dim i As Integer
> >> For i = 0 To length - 1
> >> Dim n As Integer
> >> Do
> >> n = r.Next(zero, capZ)
> >> Loop While Not(n >= zero And n <= nine Or(n >= capA And n
> >> <= capZ)) s += CChar(n)
> >> Next i
> >>
> >> Return s
> >> End Function 'RandomString
> >> End Class 'StringGenerato r
> >>
> >>
> >>
> >>
> >> Crirus wrote:
> >>> Is there a function that return some random ID like string
> >>> alphanumeric? Like this:
> >>> A35sDsd1dSGsH
> >>>
> >>> Thanks
> >>> Crirus[/color][/color]
>
>[/color]
Re: HOWTO - generate a alphanumeric string of a given length
Actually, this was converted from C# ( from an earlier version ) , so I
hadnt tested it in VB.NET.
Never mind the basic phlisophy was there.
OHM
Mick Doherty wrote:[color=blue]
> 1.1.4322.573 (VS2003)
>
> "One Handed Man" <Bombay@Duck.ne t> wrote in message
> news:bovvlj$2j1 $1@titan.btinte rnet.com...[color=green]
>> What version of .NET framework are you using
>>
>>
>>
>>
>> Mick Doherty wrote:[color=darkred]
>>> What's that CInt("0"c)? doesn't work here although Asc("0"c) does,
>>> as does Asc("0").
>>> What's that Machine.Shift.L eft? doesn't work here. I understand what
>>> it's meant to do though.(number between 0 and &HFFFFFFFF).
>>> I like that Random class, hadn't seen that before, but, according to
>>> the documentation, for best performance Random should only be
>>> created once. StringBuilder.A ppend is faster than String +=.
>>> You missed the LCase characters.
>>>
>>> My much simpler version of that same function, with default time
>>> based seeding of Random:
>>>
>>> Private r As New Random
>>> 'I think 255 characters should be enough, although there's no reason
>>> that 'you can't use Integer or Short instead of Byte, and have more.
>>> Private Function RandomString(By Val Stringlength As Byte) As String
>>>
>>> Dim s As String = _
>>> "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZabcdefgh ijklmnopqrstuvw xyz"
>>> Dim sb As New System.Text.Str ingBuilder
>>>
>>> Do
>>> sb.Append(s.Cha rs(r.Next(0, 61)))
>>> Loop While sb.Length < Stringlength
>>>
>>> Return sb.ToString
>>>
>>> End Function
>>>
>>> My version generated 100000 * 255character strings in 4 secs as
>>> opposed to yours (updated to use stringbuilder and default seeding
>>> for comparison) taking 6 secs(making the Random object a Class
>>> level Private object reduced this to 5 secs). Without Stringbuilder
>>> yours took 23 secs and 21 secs.
>>>
>>> Before that Random Class I used the old Rnd function and the fastest
>>> I could get, even using Stringbuilder, was 28 secs.
>>>
>>> So I learned something useful Today.
>>> Thankyou
>>>
>>> Mick Doherty
>>>
>>> "One Handed Man" <Bombay@Duck.ne t> wrote in message
>>> news:botppb$s58 $1@titan.btinte rnet.com...
>>>> Try this - OHM
>>>>
>>>> Public Class StringGenerator
>>>>
>>>> Public Shared Function RandomString(le ngth As Integer) As String
>>>> Const zero As Integer = CInt("0"c)
>>>> Const nine As Integer = CInt("9"c)
>>>> Const capA As Integer = CInt("A"c)
>>>> Const capZ As Integer = CInt("Z"c)
>>>>
>>>> Dim guid As Byte() = Guid.NewGuid(). ToByteArray()
>>>> Dim seed As Integer = guid(0) + Machine.Shift.L eft(guid(1),
>>>> 8) + Machine.Shift.L eft(guid(2), 16) + Machine.Shift.L eft(guid(3),
>>>> 24)
>>>>
>>>> Dim r As New Random(seed)
>>>>
>>>> Dim s As String = Nothing
>>>> Dim i As Integer
>>>> For i = 0 To length - 1
>>>> Dim n As Integer
>>>> Do
>>>> n = r.Next(zero, capZ)
>>>> Loop While Not(n >= zero And n <= nine Or(n >= capA And n
>>>> <= capZ)) s += CChar(n)
>>>> Next i
>>>>
>>>> Return s
>>>> End Function 'RandomString
>>>> End Class 'StringGenerato r
>>>>
>>>>
>>>>
>>>>
>>>> Crirus wrote:
>>>>> Is there a function that return some random ID like string
>>>>> alphanumeric? Like this:
>>>>> A35sDsd1dSGsH
>>>>>
>>>>> Thanks
>>>>> Crirus[/color][/color][/color]
Re: HOWTO - generate a alphanumeric string of a given length
So Random class is much better than rnd function?
--
Ceers,
Crirus
------------------------------
If work were a good thing, the boss would take it all from you
------------------------------
"Mick Doherty" <mdaudi100@nosp am.ntlworld.com > wrote in message
news:QaLsb.157$ Oi.395668@newsf ep1-win.server.ntli .net...[color=blue]
> What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as does
> Asc("0").
> What's that Machine.Shift.L eft? doesn't work here. I understand what it's
> meant to do though.(number between 0 and &HFFFFFFFF).
> I like that Random class, hadn't seen that before, but, according to the
> documentation, for best performance Random should only be created once.
> StringBuilder.A ppend is faster than String +=.
> You missed the LCase characters.
>
> My much simpler version of that same function, with default time based
> seeding of Random:
>
> Private r As New Random
> 'I think 255 characters should be enough, although there's no reason that
> 'you can't use Integer or Short instead of Byte, and have more.
> Private Function RandomString(By Val Stringlength As Byte) As String
>
> Dim s As String = _
> "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZabcdefgh ijklmnopqrstuvw xyz"
> Dim sb As New System.Text.Str ingBuilder
>
> Do
> sb.Append(s.Cha rs(r.Next(0, 61)))
> Loop While sb.Length < Stringlength
>
> Return sb.ToString
>
> End Function
>
> My version generated 100000 * 255character strings in 4 secs as opposed to
> yours (updated to use stringbuilder and default seeding for comparison)
> taking 6 secs(making the Random object a Class level Private object[/color]
reduced[color=blue]
> this to 5 secs). Without Stringbuilder yours took 23 secs and 21 secs.
>
> Before that Random Class I used the old Rnd function and the fastest I[/color]
could[color=blue]
> get, even using Stringbuilder, was 28 secs.
>
> So I learned something useful Today.
> Thankyou
>
> Mick Doherty
>
> "One Handed Man" <Bombay@Duck.ne t> wrote in message
> news:botppb$s58 $1@titan.btinte rnet.com...[color=green]
> > Try this - OHM
> >
> > Public Class StringGenerator
> >
> > Public Shared Function RandomString(le ngth As Integer) As String
> > Const zero As Integer = CInt("0"c)
> > Const nine As Integer = CInt("9"c)
> > Const capA As Integer = CInt("A"c)
> > Const capZ As Integer = CInt("Z"c)
> >
> > Dim guid As Byte() = Guid.NewGuid(). ToByteArray()
> > Dim seed As Integer = guid(0) + Machine.Shift.L eft(guid(1), 8) +
> > Machine.Shift.L eft(guid(2), 16) + Machine.Shift.L eft(guid(3), 24)
> >
> > Dim r As New Random(seed)
> >
> > Dim s As String = Nothing
> > Dim i As Integer
> > For i = 0 To length - 1
> > Dim n As Integer
> > Do
> > n = r.Next(zero, capZ)
> > Loop While Not(n >= zero And n <= nine Or(n >= capA And n <=[/color]
> capZ))[color=green]
> > s += CChar(n)
> > Next i
> >
> > Return s
> > End Function 'RandomString
> > End Class 'StringGenerato r
> >
> >
> >
> >
> > Crirus wrote:[color=darkred]
> > > Is there a function that return some random ID like string
> > > alphanumeric? Like this:
> > > A35sDsd1dSGsH
> > >
> > > Thanks
> > > Crirus[/color]
> >
> >[/color]
>
>[/color]
Re: HOWTO - generate a alphanumeric string of a given length
It would certainly appear that way.
If, rather than a time based seed, you want a randomized seed like OHM's
original example you can update the function as follows.
Private Function RandomString(By Val Stringlength As Byte) As String
Dim s As String = _
"0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZabcdefgh ijklmnopqrstuvw xyz"
Dim sb As New StringBuilder
'It's back inside the function but still only gets called once.
Static r As Random = Nothing
If r Is Nothing Then
'4 bytes needed to make an Integer (0 to 3).
Dim seed() As Byte = New Byte(3) {}
Dim rng As New RNGCryptoServic eProvider
'set the byte contents of seed() to random values.
rng.GetBytes(se ed)
'use BitConverter to convert the bytearray to an Integer
r = New Random(BitConve rter.ToInt32(se ed, 0))
End If
Do
sb.Append(s.Cha rs(r.Next(0, 61)))
Loop While sb.Length < Stringlength
Return sb.ToString
End Function
///
"Crirus" <Crirus@datagro up.ro> wrote in message
news:eXj1wanqDH A.3616@tk2msftn gp13.phx.gbl...[color=blue]
> So Random class is much better than rnd function?
>
> --
> Ceers,
> Crirus
>
> ------------------------------
> If work were a good thing, the boss would take it all from you
>
> ------------------------------
>
> "Mick Doherty" <mdaudi100@nosp am.ntlworld.com > wrote in message
> news:QaLsb.157$ Oi.395668@newsf ep1-win.server.ntli .net...[color=green]
> > What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as[/color][/color]
does[color=blue][color=green]
> > Asc("0").
> > What's that Machine.Shift.L eft? doesn't work here. I understand what[/color][/color]
it's[color=blue][color=green]
> > meant to do though.(number between 0 and &HFFFFFFFF).
> > I like that Random class, hadn't seen that before, but, according to the
> > documentation, for best performance Random should only be created once.
> > StringBuilder.A ppend is faster than String +=.
> > You missed the LCase characters.
> >
> > My much simpler version of that same function, with default time based
> > seeding of Random:
> >
> > Private r As New Random
> > 'I think 255 characters should be enough, although there's no reason[/color][/color]
that[color=blue][color=green]
> > 'you can't use Integer or Short instead of Byte, and have more.
> > Private Function RandomString(By Val Stringlength As Byte) As String
> >
> > Dim s As String = _
> > "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZabcdefgh ijklmnopqrstuvw xyz"
> > Dim sb As New System.Text.Str ingBuilder
> >
> > Do
> > sb.Append(s.Cha rs(r.Next(0, 61)))
> > Loop While sb.Length < Stringlength
> >
> > Return sb.ToString
> >
> > End Function
> >
> > My version generated 100000 * 255character strings in 4 secs as opposed[/color][/color]
to[color=blue][color=green]
> > yours (updated to use stringbuilder and default seeding for comparison)
> > taking 6 secs(making the Random object a Class level Private object[/color]
> reduced[color=green]
> > this to 5 secs). Without Stringbuilder yours took 23 secs and 21 secs.
> >
> > Before that Random Class I used the old Rnd function and the fastest I[/color]
> could[color=green]
> > get, even using Stringbuilder, was 28 secs.
> >
> > So I learned something useful Today.
> > Thankyou
> >
> > Mick Doherty
> >
> > "One Handed Man" <Bombay@Duck.ne t> wrote in message
> > news:botppb$s58 $1@titan.btinte rnet.com...[color=darkred]
> > > Try this - OHM
> > >
> > > Public Class StringGenerator
> > >
> > > Public Shared Function RandomString(le ngth As Integer) As String
> > > Const zero As Integer = CInt("0"c)
> > > Const nine As Integer = CInt("9"c)
> > > Const capA As Integer = CInt("A"c)
> > > Const capZ As Integer = CInt("Z"c)
> > >
> > > Dim guid As Byte() = Guid.NewGuid(). ToByteArray()
> > > Dim seed As Integer = guid(0) + Machine.Shift.L eft(guid(1), 8) +
> > > Machine.Shift.L eft(guid(2), 16) + Machine.Shift.L eft(guid(3), 24)
> > >
> > > Dim r As New Random(seed)
> > >
> > > Dim s As String = Nothing
> > > Dim i As Integer
> > > For i = 0 To length - 1
> > > Dim n As Integer
> > > Do
> > > n = r.Next(zero, capZ)
> > > Loop While Not(n >= zero And n <= nine Or(n >= capA And n <=[/color]
> > capZ))[color=darkred]
> > > s += CChar(n)
> > > Next i
> > >
> > > Return s
> > > End Function 'RandomString
> > > End Class 'StringGenerato r
> > >
> > >
> > >
> > >
> > > Crirus wrote:
> > > > Is there a function that return some random ID like string
> > > > alphanumeric? Like this:
> > > > A35sDsd1dSGsH
> > > >
> > > > Thanks
> > > > Crirus
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]
Comment