browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need C# / C Sharp help?

Get answers from our community of C# / C Sharp experts on BYTES! It's free.

C# random alphanumeric strings ?

bitshift
Guest
 
Posts: n/a
#1: Jul 2 '07
Anyone got an example ?





Tom Spink
Guest
 
Posts: n/a
#2: Jul 2 '07

re: C# random alphanumeric strings ?


bitshift wrote:
Quote:
Anyone got an example ?
asdxcgfd

--
Tom Spink
University of Edinburgh
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Jul 2 '07

re: C# random alphanumeric strings ?


On Jul 2, 3:12 pm, "bitshift" <j...@aol.comwrote:
Quote:
Anyone got an example ?
1) Hard-code a string with all the characters you want to include
2) Use a single instance of Random (with appropriate locking if you're
going to use it from many threads)
3) Create a StringBuilder of the right length
4) In a for loop, append the next random character by using
Random.Next, passing in the string's length, and using string's
indexer
5) Call ToString on the StringBuilder

Jon

Tom Spink
Guest
 
Posts: n/a
#4: Jul 2 '07

re: C# random alphanumeric strings ?


Jon Skeet [C# MVP] wrote:
Quote:
On Jul 2, 3:12 pm, "bitshift" <j...@aol.comwrote:
Quote:
>Anyone got an example ?
>
1) Hard-code a string with all the characters you want to include
2) Use a single instance of Random (with appropriate locking if you're
going to use it from many threads)
3) Create a StringBuilder of the right length
4) In a for loop, append the next random character by using
Random.Next, passing in the string's length, and using string's
indexer
5) Call ToString on the StringBuilder
>
Jon
Hi Jon,

Great minds think alike.

--
Tom Spink
University of Edinburgh
Tom Spink
Guest
 
Posts: n/a
#5: Jul 2 '07

re: C# random alphanumeric strings ?


bitshift wrote:
Quote:
Anyone got an example ?
Also,

///
using System;
using System.Text;

....

public string GetRandomString (Random rnd, int length)
{
string charPool
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz1234567890";
StringBuilder rs = new StringBuilder();

while (length-- 0)
rs.Append(charPool[(int)(rnd.NextDouble() * charPool.Length)]);

return rs.ToString();
}
///

Watch out for wrapping on the charPool line! Note, the method takes an
instance of the Random class, which you can instantiate with new Random().

The reason it doesn't do this itself is that (I think) the Random class uses
a timer as it's entropy source, and if you try to generate a large sequence
of random numbers quickly, the seeds will match and you'll get matching
random sequences.

So the way I tested this was:

///
public static void Main ()
{
int i = 50;
Random rnd = new Random();
while (i-- 0)
Console.WriteLine(GetRandomString(rnd, 10));
}
///

--
Tom Spink
University of Edinburgh
tomk148@gmail.com
Guest
 
Posts: n/a
#6: Jul 2 '07

re: C# random alphanumeric strings ?


Great minds think alike.

similar ones at least

=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
 
Posts: n/a
#7: Jul 2 '07

re: C# random alphanumeric strings ?


Here is some code that will generate a cryptograhically random unique string
of any length you want:

using System.Security.Cryptography;
using System.Text;

namespace UniqueKey
{
public class KeyGenerator
{
public static string GetUniqueKey(int maxSize)
{
char[] chars = new char[62];
chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890".ToCharArray();
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
data = new byte[maxSize];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(maxSize);
foreach (byte b in data)
{
result.Append(chars[b%(chars.Length - 1)]);
}
return result.ToString();
}
}
}

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com



"bitshift" wrote:
Quote:
Anyone got an example ?
>
>
>
rossum
Guest
 
Posts: n/a
#8: Jul 2 '07

re: C# random alphanumeric strings ?


On Mon, 2 Jul 2007 09:12:40 -0500, "bitshift" <jobob@aol.comwrote:
Quote:
>Anyone got an example ?
>
Two questions:

- do you want characters in the string to repeat or not?
- is the output going to be used for cryptographic/security purposes?

rossum

=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
 
Posts: n/a
#9: Jul 3 '07

re: C# random alphanumeric strings ?


bitshift wrote:
Quote:
Anyone got an example ?
private static Random rng = new Random();
public static string newPassword(int l)
{
char[] valid = { 'A', 'B', 'C', '2', '3', '4' };
StringBuilder sb = new StringBuilder("");
for(int i = 0; i < l; i++)
{
sb.Append(valid[rng.Next(valid.Length)]);
}
return sb.ToString();
}

Arne
=?UTF-8?B?QXJuZSBWYWpow7hq?=
Guest
 
Posts: n/a
#10: Jul 3 '07

re: C# random alphanumeric strings ?


Peter Bromberg [C# MVP] wrote:
Quote:
char[] chars = new char[62];
chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890".ToCharArray();
Why the new char[62] ?

Arne
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
 
Posts: n/a
#11: Jul 3 '07

re: C# random alphanumeric strings ?


:-)
--
Milosz


"Tom Spink" wrote:
Quote:
bitshift wrote:
>
Quote:
Anyone got an example ?
>
asdxcgfd
>
--
Tom Spink
University of Edinburgh
>
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
 
Posts: n/a
#12: Jul 3 '07

re: C# random alphanumeric strings ?


I suppose you could do it this way if you prefer:
char[] chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890".ToCharArray();

Actually, there are 64 characters in the string. Good catch.

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com



"Arne Vajhøj" wrote:
Quote:
Peter Bromberg [C# MVP] wrote:
Quote:
char[] chars = new char[62];
chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890".ToCharArray();
>
Why the new char[62] ?
>
Arne
>
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
 
Posts: n/a
#13: Jul 3 '07

re: C# random alphanumeric strings ?


whoops, my bad. There are indeed 62 characters in the string.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com



"Arne Vajhøj" wrote:
Quote:
Peter Bromberg [C# MVP] wrote:
Quote:
char[] chars = new char[62];
chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890".ToCharArray();
>
Why the new char[62] ?
>
Arne
>
Peter Duniho
Guest
 
Posts: n/a
#14: Jul 3 '07

re: C# random alphanumeric strings ?


On Tue, 03 Jul 2007 06:26:01 -0700, Peter Bromberg [C# MVP]
<pbromberg@yahoo.yabbadabbadoo.comwrote:
Quote:
I suppose you could do it this way if you prefer:
char[] chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890".ToCharArray();
I think the point is, why _wouldn't_ you prefer to do it that way?

Creating an empty 62-element array doesn't seem useful. That char[]
instance just gets thrown away when the subsequent assignment is made. So
why do it at all?

I could be missing something, but it seems to me that that line of code is
just superfluous.

Pete
=?UTF-8?B?QXJuZSBWYWpow7hq?=
Guest
 
Posts: n/a
#15: Jul 4 '07

re: C# random alphanumeric strings ?


Peter Bromberg [C# MVP] wrote:
Quote:
"Arne Vajhøj" wrote:
Quote:
>Peter Bromberg [C# MVP] wrote:
Quote:
>> char[] chars = new char[62];
>> chars =
>>"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU VWXYZ1234567890".ToCharArray();
>Why the new char[62] ?
whoops, my bad. There are indeed 62 characters in the string.
It does not matter. The only difference is whether 62 or
64 chars get GC'ed.

Arne
Sam Raj
Guest
 
Posts: n/a
#16: Oct 31 '08

re: C# random alphanumeric strings ?


http://www.dotnetspider.com/resource...-string-C.aspx
Closed Thread