Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

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

C#
var charList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var randomNum = new Random();
var result = new string(
    Enumerable.Repeat(charList, 8)
              .Select(s => s[randomNum.Next(s.Length)])
              .ToArray());
 
Share this answer
 
http://www.obviex.com/Samples/Password.aspx[^]


Hope this helps if yes then accept and vote the answer .
--Rahul D.
 
Share this answer
 
Just try this:

C#
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();
        }
    }
}
 
Share this answer
 
JavaScript
<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>
 
Share this answer
 
Another one (No Linq, uses StringBuilder)

C#
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:

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900