Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,836 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,578 people online right now. Registration is fast and FREE... Join Now!




create random numbers and alphabet

 
Reply to this topicStart new topic

create random numbers and alphabet

king of loop
1 Oct, 2008 - 03:34 AM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16



Thanked: 1 times
My Contributions

Hi,


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

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Create Random Numbers And Alphabet
1 Oct, 2008 - 04:40 AM
Post #2

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 375



Thanked: 19 times
Dream Kudos: 25
My Contributions
csharp

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: 1 Oct, 2008 - 04:43 AM
User is offlineProfile CardPM
+Quote Post

king of loop
RE: Create Random Numbers And Alphabet
1 Oct, 2008 - 07:41 AM
Post #3

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16



Thanked: 1 times
My Contributions
Hi, eclipsed4utoo

thx for reply .I have written this code becuse only returns numbers and alphabet .

CODE


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


User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Create Random Numbers And Alphabet
1 Oct, 2008 - 07:48 AM
Post #4

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 375



Thanked: 19 times
Dream Kudos: 25
My Contributions
QUOTE(king of loop @ 1 Oct, 2008 - 11:41 AM) *

Hi, eclipsed4utoo

thx for reply .I have written this code becuse only returns numbers and alphabet .

CODE


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.
User is offlineProfile CardPM
+Quote Post

ragingben
RE: Create Random Numbers And Alphabet
7 Oct, 2008 - 05:17 AM
Post #5

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 30


My Contributions
Hi, heres a bit of code I whipped together, should sort you out!

CODE

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!
User is offlineProfile CardPM
+Quote Post

Ghasem.karimi
RE: Create Random Numbers And Alphabet
17 Oct, 2008 - 04:05 AM
Post #6

New D.I.C Head
*

Joined: 16 Sep, 2008
Posts: 18


My Contributions
thx ragingben 4 reply smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Create Random Numbers And Alphabet
17 Oct, 2008 - 09:13 AM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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

csharp

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

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 04:30PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month