I was trying to find that if the Random Numbers being generated by the Random function are truly Random or not. So I wrote this small piece of code. When I debug the application it never goes into the else of the IsUnique method. But when I run this application "without debugging" it always print "This is not a random Number". Any ideas !
using System;
using System.Collections;
namespace RandomNumberTest
{
class Class1
{
private static ArrayList randomList = new ArrayList();
private static int notRandom = 0;
[STAThread]
static void Main(string[] args)
{
for(int i=0;i<=10;i++)
{
CreateRandomNumber();
}
}
static void CreateRandomNumber()
{
Random d = new Random();
int uniqueKey = d.Next();
IsUnique(uniqueKey);
}
static void IsUnique(int uniqueKey)
{
if( ! randomList.Contains((int) uniqueKey))
{
randomList.Add(uniqueKey);
Console.WriteLine(uniqueKey);
}
else
{
notRandom++;
Console.WriteLine("This is not a random Password:"+uniqueKey);
Console.WriteLine("Total NOT RANDOM Numbers: "+notRandom);
}
}
}
}
This is a small piece of code that generates Random Passwords:
string alphabets = "abcdefghijklmnopqrstuvwxyz";
string numbers = "01234567890123456789012345";
StringBuilder password = new StringBuilder();
Random r = new Random();
for(int j=0; j<=20;j++)
{
for(int i=0;i<=5;i++)
{
password.Append(alphabets[r.Next(alphabets.Length)]);
password.Append(numbers[r.Next(numbers.Length)]);
}
Response.Write(password.ToString());
password.Remove(0,password.Length);
}