Using ASP.net (4) with C# (2008 & 2010) also Silverlight
Hello Everyone!
I would like to generate a combination of random letters and numbers.
I need 3 of each that must not be duplicated at any time.
The desired result would look something like "ABC123" when output to a label.
Thanks in advance for your input
Don Taylor
Loading
VulpesPosted Mar 31, 2011, 9:08 AM
using System;
using System.Collections.Generic;
class Test
{
static void Main()
{
string result = GenerateRandomAlphanumerics();
Console.WriteLine(result);
Console.ReadKey();
}
static Random rand = new Random();
static string GenerateRandomAlphanumerics()
{
List
int count = 0;
while (count < 3)
{
char next = (char)rand.Next(65, 91); // endpoint needs to be one more than 'Z'
if (!list.Contains(next))
{
list.Add(next);
count++;
}
}
count = 0;
while (count < 3)
{
char next = (char)rand.Next(48, 58); // endpoint needs to be one more than '9'
if (!list.Contains(next))
{
list.Add(next);
count++;
}
}
return new string(list.ToArray());
}
}
CrishPosted Apr 25, 2011, 8:09 AM
string oustr="";
for(int i=0;i<7;i++)
{
val=rnd.Next(0,62);
if(val<26)
outstr+=(char)(val+'a');
else if(val<52)
outstr+=(char)('A' + (val + 'a'));
else
{
outstr+=(char)('o' + (val-52));
}
}
http://www.axistechnolabs.com/technology/ecommerce-development.aspx]ecommerce development company[/url]
VulpesPosted Apr 25, 2011, 7:55 AM
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string GenerateRandomAlphanumerics() // change to public instance rather than private static
{
Random rand = new Random(); // put this line in here - don't now need the static field
List
int count = 0;
while (count < 3)
{
char next = (char)rand.Next(65, 91); // endpoint needs to be one more than 'Z'
if (!list.Contains(next))
{
list.Add(next);
count++;
}
}
count = 0;
while (count < 3)
{
char next = (char)rand.Next(48, 58); // endpoint needs to be one more than '9'
if (!list.Contains(next))
{
list.Add(next);
count++;
}
}
return new string(list.ToArray());
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = GenerateRandomAlphanumerics();
}
}
don taylorPosted Apr 25, 2011, 6:12 AM
Thank you for the code, it works fine as a console aplication. How would I output this to a label in Visual studio. I have tried putting a button and a label on a form but VS just sends me round and round with errors.
Thanks again
Regards
Don Taylor.