Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
hi please help me to how generate alphanumeric strings/code as its done by some website in forget password type situation . where generated strings/character is shipped to concerned person.any links to tutorial/videos would be hepfull

thanks
anoop
Posted 21-Feb-12 20:23pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In .NET, we can generate random alphanumeric characters using LInQ

var charList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var randomNum = new Random();
var result = new string(
    Enumerable.Repeat(charList, 8)
              .Select(s => s[randomNum.Next(s.Length)])
              .ToArray());
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

http://www.obviex.com/Samples/Password.aspx[^]


Hope this helps if yes then accept and vote the answer .
--Rahul D.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Just try this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string charList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var randomNum = new Random();
            string result = new string(
                Enumerable.Repeat(charList, 30)
                          .Select(s => s[randomNum.Next(s.Length)])
                          .ToArray());
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

<script language="javascript" type="text/javascript">
        function GetString(txtCode) {
            document.getElementById(txtCode).value = RamdomString(10);
            return false;
        }
        //Random String Generator//
        function RamdomString(intLen) {
            var strRet = "";
            var iCntr = 0;
            var rndNo = 0;
            var arrCharacters = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
            for (iCntr = 0; iCntr < intLen; iCntr++) {
                rndNo = Math.floor((61 - 1 + 1) * Math.random() + 1);
                strRet = strRet + arrCharacters[rndNo];
            }
            return strRet;
        }
    </script>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Another one (No Linq, uses StringBuilder)

public static string GetRandomString(char[] achCharacters, int iLength, Random rand)
{
    StringBuilder sb = new StringBuilder(iLength);
    for (int i = 0; i < iLength; i++)
        sb.Append(achCharacters[rand.Next(0, achCharacters.Length)]);
    return sb.ToString();
}

Example Usage:

 
string strUsableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!\"§$%&/()=?";
            Random rand = new Random();
            string strRandom = GetRandomString(strUsableChars.ToCharArray(), 10, rand);

Why inject Random-instance from outside?
- If I'd create it inside the function, fast successive calls could yield the same strings.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 300
1 Sergey Alexandrovich Kryukov 204
2 PIEBALDconsult 180
3 Suvendu Shekhar Giri 115
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,434
1 OriginalGriff 6,033
2 Peter Leow 2,534
3 Maciej Los 2,268
4 Abhinav S 2,264


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 22 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100