Random Number and String Generators

  • Thread starter Thread starter BillG
  • Start date Start date
B

BillG

Hi,
Does anyone know of a site or have code for a function that will generate a
random string or random number?
I need one where I can tell it what type of value I need and where I can set
the length of the desired output.

Thanks
 
Does anyone know of a site or have code for a function that will generate a
random string or random number?
I need one where I can tell it what type of value I need and where I can set
the length of the desired output.

Well, System.Random is the normal random number generator to use
in .NET. It will give you numbers - from there it's easy to create
random strings.

You should only create a single instance of System.Random and reuse it
rather than creating a new one each time you need it, by the way -
otherwise you may get repeated patterns because the instance is
initialized with the current time as the seed.

Jon
 
Hello,

Take a look.

/// Verify the random string
public string RandomString
{
get
{
string str = "";
while (true)
{
str = InternalRandomString;

if (! m_lastRandomString.Contains(str))
{
m_lastRandomString.Add( str);
break;
}
}


return str;
}
}

/// <summary>
/// Generates a m_random string with the given length
/// </summary>
/// <returns>Random string</returns>
private string InternalRandomString
{
get
{
StringBuilder builder = new StringBuilder();

char ch;

int size = m_random.Next(2 , 8);

for (int i = 0 ; i < size ; i++)
{
ch = Convert.ToChar(m_random.Next(97 , 122));

builder.Append(ch);
}

return builder.ToString();
}
}
 
The repeated pattern was what I was getting. How can I go about stopping
this from happening?

I've got the random generators in a separate C# file from rest of code, with
intention of using it in other apps if needed.

Tried to add the 'Random r = new Random();' to top of file just inside the
public class section but couldn't get it to be seen in the procedures within
it.



thanks
 
The repeated pattern was what I was getting. How can I go about stopping
this from happening?

I've got the random generators in a separate C# file from rest of code, with
intention of using it in other apps if needed.

Tried to add the 'Random r = new Random();' to top of file just inside the
public class section but couldn't get it to be seen in the procedures within
it.

We'd have to see the exact code to know what's wrong.

You might want to use my StaticRandom class - see
http://pobox.com/~skeet/csharp/miscutil/usage/staticrandom.html
and
http://pobox.com/~skeet/csharp/miscutil

Jon
 
Here is the code I am using to generate random strings

public static string RandomString(int size, string legalChars)
{
Random r = new Random(1);
//string legalChars =
"123456789abcdefghijklmnopqrstuvwxzy";//ABCDEFGHIJKLMNOPQRSTUVWXZY";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++)
sb.Append(legalChars.Substring(r.Next(0, legalChars.Length -
1), 1));
return sb.ToString();
}

Thanks
 
Here is the code I am using to generate random strings

public static string RandomString(int size, string legalChars)
{
Random r = new Random(1);
//string legalChars =
"123456789abcdefghijklmnopqrstuvwxzy";//ABCDEFGHIJKLMNOPQRSTUVWXZY";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++)
sb.Append(legalChars.Substring(r.Next(0, legalChars.Length -
1), 1));
return sb.ToString();
}

You're specifying the same seed every time, so it will always generate
the same string.

Additionally, you're never going to see a "Z" in the string. You
should just use:

sb.Append(legalChars[r.Next(legalChars.Length)]);

Jon
 
aaa
Hi,
Does anyone know of a site or have code for a function that will generate a
random string or random number?
I need one where I can tell it what type of value I need and where I can set
the length of the desired output.

Thanks
On Wednesday, August 22, 2007 4:52 AM Jon Skeet [C# MVP] wrote:

Well, System.Random is the normal random number generator to use
in .NET. It will give you numbers - from there it's easy to create
random strings.

You should only create a single instance of System.Random and reuse it
rather than creating a new one each time you need it, by the way -
otherwise you may get repeated patterns because the instance is
initialized with the current time as the seed.

Jon
On Wednesday, August 22, 2007 5:57 AM Jon Skeet [C# MVP] wrote:

We'd have to see the exact code to know what's wrong.

You might want to use my StaticRandom class - see
http://pobox.com/~skeet/csharp/miscutil/usage/staticrandom.html
and
http://pobox.com/~skeet/csharp/miscutil

Jon
On Wednesday, August 22, 2007 10:35 AM Jon Skeet [C# MVP] wrote:

You're specifying the same seed every time, so it will always generate
the same string.

Additionally, you're never going to see a "Z" in the string. You
should just use:

sb.Append(legalChars[r.Next(legalChars.Length)]);

Jon
 
public static string RandomString(int size)
{
StringBuilder sb = new StringBuilder();
char cha;
string fromTo = "0z";
for (int i = 0 ; i<size ; i ++)
{
cha = Convert.ToChar(RandomNumber(fromTo[0],fromTo[1]));
sb.Append(cha);
System.Threading.Thread.Sleep(20);
}
return sb.ToString();
}
Hi,
Does anyone know of a site or have code for a function that will generate a
random string or random number?
I need one where I can tell it what type of value I need and where I can set
the length of the desired output.

Thanks
On Wednesday, August 22, 2007 4:52 AM Jon Skeet [C# MVP] wrote:

Well, System.Random is the normal random number generator to use
in .NET. It will give you numbers - from there it's easy to create
random strings.

You should only create a single instance of System.Random and reuse it
rather than creating a new one each time you need it, by the way -
otherwise you may get repeated patterns because the instance is
initialized with the current time as the seed.

Jon
On Wednesday, August 22, 2007 5:57 AM Jon Skeet [C# MVP] wrote:

We'd have to see the exact code to know what's wrong.

You might want to use my StaticRandom class - see
http://pobox.com/~skeet/csharp/miscutil/usage/staticrandom.html
and
http://pobox.com/~skeet/csharp/miscutil

Jon
On Wednesday, August 22, 2007 10:35 AM Jon Skeet [C# MVP] wrote:

You're specifying the same seed every time, so it will always generate
the same string.

Additionally, you're never going to see a "Z" in the string. You
should just use:

sb.Append(legalChars[r.Next(legalChars.Length)]);

Jon
 
Back
Top