6 Replies - 15512 Views - Last Post: 17 October 2008 - 10:13 AM Rate Topic: -----

#1 king of loop  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 15-June 08

create random numbers and alphabet

Posted 01 October 2008 - 04:34 AM

Hi,


How to achieve random numbers and alphabet in c# similiar to this code (G87kM4o) Any help would be highly appreciated

Is This A Good Question/Topic? 1

Replies To: create random numbers and alphabet

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

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

Was This Post Helpful? 0
  • +
  • -

#3 king of loop  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 16
  • Joined: 15-June 08

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 .


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();



Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: create random numbers and alphabet

Posted 01 October 2008 - 08:48 AM

View Postking 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 .


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.
Was This Post Helpful? 0
  • +
  • -

#5 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

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!

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!
Was This Post Helpful? 0
  • +
  • -

#6 Ghasem.karimi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-September 08

Re: create random numbers and alphabet

Posted 17 October 2008 - 05:05 AM

thx ragingben 4 reply :)
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

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();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1