Hi,
How to achieve random numbers and alphabet in c# similiar to this code (G87kM4o) Any help would be highly appreciated
create random numbers and alphabet
Page 1 of 16 Replies - 13888 Views - Last Post: 17 October 2008 - 10:13 AM
Replies To: create random numbers and alphabet
#2
Re: create random numbers and alphabet
Posted 01 October 2008 - 05:40 AM
StringBuilder sb = new StringBuilder();
Random rand = new Random();
for (int i = 1; i <= 8; i++)
{
sb.Append((char)rand.Next(48, 123));
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
this will create a string of 8 random letters/numbers, though it will still give a couple of special characters.
This post has been edited by eclipsed4utoo: 01 October 2008 - 05:43 AM
#3
Re: create random numbers and alphabet
Posted 01 October 2008 - 08:41 AM
Hi, eclipsed4utoo
thx for reply .I have written this code becuse only returns numbers and alphabet .
thx for reply .I have written this code becuse only returns numbers and alphabet .
Random rnd = new Random();
int val;
string outstr = "";
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 - 26));
else
{
outstr += (char)('0' + (val - 52));
}
}
label1.Text = outstr.Trim();
#4
Re: create random numbers and alphabet
Posted 01 October 2008 - 08:48 AM
king of loop, on 1 Oct, 2008 - 11:41 AM, said:
Hi, eclipsed4utoo
thx for reply .I have written this code becuse only returns numbers and alphabet .
thx for reply .I have written this code becuse only returns numbers and alphabet .
Random rnd = new Random();
int val;
string outstr = "";
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 - 26));
else
{
outstr += (char)('0' + (val - 52));
}
}
label1.Text = outstr.Trim();
look at this...
http://www.asciitable.com/
the "dec" column is the number that you need the random number generator to generate for those specific letters. if you stop at 64(which is what you are doing, then you won't have any letters.
#5
Re: create random numbers and alphabet
Posted 07 October 2008 - 06:17 AM
Hi, heres a bit of code I whipped together, should sort you out!
Hope it helps!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static List<Char> randomNumbers(Int32 totalNumbers)
{
// create a new instance of random (default seed)
Random rand = new Random();
// create a new list of characters
List<Char> outputList = new List<Char>();
// keep adding random characters to list
for (Int32 index = 0; index < totalNumbers; index++)
{
// random result is 1 add letter, else number
if(rand.Next(0,2) == 1)
{
// add a letter (ASCII 65-90 = A-Z)
outputList.Add((Char)rand.Next(65,90));
}
else
{
// add a number (ASCII 48-50 = 0-9)
outputList.Add((Char)rand.Next(48, 57));
}
}
// return list of random characters
return outputList;
}
static void Main(string[] args)
{
foreach(Char element in randomNumbers(10))
{
Console.WriteLine(element);
}
Console.ReadKey();
}
}
}
Hope it helps!
#6
Re: create random numbers and alphabet
Posted 17 October 2008 - 05:05 AM
thx ragingben 4 reply
#7
Re: create random numbers and alphabet
Posted 17 October 2008 - 10:13 AM
Here is a much shorter version that I use for generating my CAPTCHA code. You pass it the length you wish to have and it returns a random code that length
public string GenerateRandomCode(ref int length)
{
string charPool = "ABCDEFGOPQRSTUVWXY1234567890ZabcdefghijklmHIJKLMNnopqrstuvwxyz";
StringBuilder rs = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++)
{
rs.Append(charPool[(int)(random.NextDouble() * charPool.Length)]);
}
return rs.ToString();
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|