Is there a function that return some random ID like string alphanumeric?
Like this:
A35sDsd1dSGsH
Thanks
Crirus 10 2574
Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length 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.Left(guid(1), 8) +
Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(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 'StringGenerator
Crirus wrote: Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH
Thanks Crirus
HI OHM,
Not only random but with build in Ohandedcator.
:-))
Cor
In article <#5**************@TK2MSFTNGP10.phx.gbl>, Crirus wrote: Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH
Thanks Crirus
How long do you want it?
Dim id As String = Guid.NewGuid().ToString().Replace("-", String.Empty)
Console.WriteLine(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).
--
Tom Shelton
MVP [Visual Basic]
LOL
Cor wrote: HI OHM,
Not only random but with build in Ohandedcator.
:-))
Cor
What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as does
Asc("0").
What's that Machine.Shift.Left? 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.Append 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(ByVal Stringlength As Byte) As String
Dim s As String = _
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz"
Dim sb As New System.Text.StringBuilder
Do
sb.Append(s.Chars(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" <Bo****@Duck.net> wrote in message
news:bo**********@titan.btinternet.com... Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length 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.Left(guid(1), 8) + Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(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 'StringGenerator
Crirus wrote: Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH
Thanks Crirus
What version of .NET framework are you using
Mick Doherty wrote: What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as does Asc("0"). What's that Machine.Shift.Left? 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.Append 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(ByVal Stringlength As Byte) As String
Dim s As String = _ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz" Dim sb As New System.Text.StringBuilder
Do sb.Append(s.Chars(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" <Bo****@Duck.net> wrote in message news:bo**********@titan.btinternet.com... Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length 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.Left(guid(1), 8) + Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(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 'StringGenerator
Crirus wrote: Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH
Thanks Crirus
1.1.4322.573 (VS2003)
"One Handed Man" <Bo****@Duck.net> wrote in message
news:bo**********@titan.btinternet.com... What version of .NET framework are you using
Mick Doherty wrote: What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as does Asc("0"). What's that Machine.Shift.Left? 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.Append 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(ByVal Stringlength As Byte) As String
Dim s As String = _ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz" Dim sb As New System.Text.StringBuilder
Do sb.Append(s.Chars(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" <Bo****@Duck.net> wrote in message news:bo**********@titan.btinternet.com... Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length 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.Left(guid(1), 8) + Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(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 'StringGenerator
Crirus wrote: Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH
Thanks Crirus
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: 1.1.4322.573 (VS2003)
"One Handed Man" <Bo****@Duck.net> wrote in message news:bo**********@titan.btinternet.com... What version of .NET framework are you using
Mick Doherty wrote: What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as does Asc("0"). What's that Machine.Shift.Left? 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.Append 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(ByVal Stringlength As Byte) As String
Dim s As String = _ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz" Dim sb As New System.Text.StringBuilder
Do sb.Append(s.Chars(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" <Bo****@Duck.net> wrote in message news:bo**********@titan.btinternet.com... Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length 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.Left(guid(1), 8) + Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(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 'StringGenerator
Crirus wrote: > Is there a function that return some random ID like string > alphanumeric? Like this: > A35sDsd1dSGsH > > Thanks > Crirus
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" <md*******@nospam.ntlworld.com> wrote in message
news:Qa*****************@newsfep1-win.server.ntli.net... What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as does Asc("0"). What's that Machine.Shift.Left? 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.Append 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(ByVal Stringlength As Byte) As String
Dim s As String = _ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz" Dim sb As New System.Text.StringBuilder
Do sb.Append(s.Chars(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" <Bo****@Duck.net> wrote in message news:bo**********@titan.btinternet.com... Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length 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.Left(guid(1), 8) + Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(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 'StringGenerator
Crirus wrote: Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH
Thanks Crirus
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.
\\\
Imports System.Security.Cryptography
Imports System.Text
Private Function RandomString(ByVal Stringlength As Byte) As String
Dim s As String = _
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz"
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 RNGCryptoServiceProvider
'set the byte contents of seed() to random values.
rng.GetBytes(seed)
'use BitConverter to convert the bytearray to an Integer
r = New Random(BitConverter.ToInt32(seed, 0))
End If
Do
sb.Append(s.Chars(r.Next(0, 61)))
Loop While sb.Length < Stringlength
Return sb.ToString
End Function
///
"Crirus" <Cr****@datagroup.ro> wrote in message
news:eX**************@tk2msftngp13.phx.gbl... 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" <md*******@nospam.ntlworld.com> wrote in message news:Qa*****************@newsfep1-win.server.ntli.net... What's that CInt("0"c)? doesn't work here although Asc("0"c) does, as
does Asc("0"). What's that Machine.Shift.Left? 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.Append 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(ByVal Stringlength As Byte) As String
Dim s As String = _ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz" Dim sb As New System.Text.StringBuilder
Do sb.Append(s.Chars(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" <Bo****@Duck.net> wrote in message news:bo**********@titan.btinternet.com... Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length 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.Left(guid(1), 8) + Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(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 'StringGenerator
Crirus wrote: > Is there a function that return some random ID like string > alphanumeric? Like this: > A35sDsd1dSGsH > > Thanks > Crirus
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: wolfgang haefelinger |
last post by:
Greetings,
I'm trying to read (japanese) chars from a file. While doing so
I encounter that a char with length 2 is returned. Is this to be
expected or is there something wrong?
Basically...
|
by: Wee |
last post by:
String.value.length and String.value doesn't work together in a single
function in IE. I've no problem running either of this, but not together at
the same time.
var...
|
by: ironcito |
last post by:
Hello!
I'm looking for a way to have a field in my database that will
automatically be filled with a random 4-character alphanumeric string
every time I enter a new record. Like an autonumber...
|
by: chiara |
last post by:
Hi everybody!
I am just at the beginning as a programmer, so maybe this is a stupid
question...Anyway,I need to write a function in C to generate generate
all possible strings of given length...
|
by: Pierre |
last post by:
I need to check the length of a string in a textbox control. I used
RegularExpressionValidator with ".{0,20}" to check if the string is between
0 and 20 characters. Is it possible to use the other...
|
by: Hemanth |
last post by:
Hello,
How do I find the first and last non-numeric char positions (using
regexp) in an alphanumeric string?
For example,
99ABC1A => should return 2, 6
DE8A1 => should return 0, 3
Thanks,
|
by: dohyohdohyoh |
last post by:
I have a programming question to generate an ordered list of
alphanumeric strings of length 4.
two alphabets rest numberst, etc.
EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9...
|
by: Mufasa |
last post by:
Is there anyway to generate a string that contains the name of the
namespace, the object name and then the function/procedure name that is
currently running.
I want to use this for logging...
|
by: ghjk |
last post by:
I want to expire user account in my php application. Below is my code . But i got "mktime() expects parameter 6 to be long, string given in ". Could anyone tell me what is wrong? Please..
...
|
by: shalabh6 |
last post by:
Hi,
A js file containing a function which is passing an alphanumeric string to another function in the same file, the second funtion requires 4 parameters to pass from the first function.
The...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |