SIGN UP MEMBER LOGIN:    
ARTICLE

Generate Unique Number in C#

Posted by Nanu Jogi Articles | C# Language January 09, 2000
Generate a unique number using random class and display it on the console.
Reader Level:

This program generates Unique Random Numbers and displays on the console.

Editor Used: Antechinus, http://www.c-point.com.

Debugger: dbgurt.exe

Thanks Saurabh for help and guidance and thanks Mahesh for loading my tutorial. And of course thanks to you for reading it you know better who you are :)

class testrandom
{
public static void Main(string[] args)
{
int intno;
if (args.Length == 0)
{
Console.WriteLine ("Please enter a parameter eg. unique 5");
return;
}
else
{
intno = Int32.Parse (args[0]);
if (intno < 1)
{
// Check to see if user has entered value >= 1 because my LowerBound is hardcoded to 1
Console.WriteLine ("Enter value greater than or equal to 1" );
return;
}
}
unique_random generateit =
new unique_random();
generateit.create_random(intno);
}
}
class unique_random
{
public void create_random (int passed_intno)
{
// Remember: C# Requires all variables Initialized.... so lets initialize variable used
int
LowerBound = 1;
int UpperBound = passed_intno;
bool firsttime = true;
int starti = 0;
int[] vararray;
vararray =
new int[UpperBound];
// Random Class used here
Random randomGenerator = new Random (DateTime.Now.Millisecond);
do
{
int nogenerated = randomGenerator.Next(LowerBound, UpperBound+1);
// Note: randomGenerator.Next generates no. to UpperBound - 1 hence +1
// .... i got stuck at this pt & had to use the debugger.
if (firsttime) // if (firsttime == true)
{
vararray[starti] = nogenerated;
// we simply store the nogenerated in vararray
firsttime =
false;
starti++;
}
else // if (firsttime == false)
{
bool duplicate_flag = CheckDuplicate(nogenerated, starti, vararray); // call to check in array
if (!duplicate_flag) // duplicate_flag == false
{
vararray[starti] = nogenerated;
starti++;
}
}
}
while (starti < UpperBound);
PrintArray (vararray);
// Print the array
}
public bool CheckDuplicate (int newrandomNum, int loopcount, int [] function_array)
{
bool temp_duplicate = false;
for (int j = 0; j < loopcount; j++)
{
if (function_array[j] == newrandomNum)
{
temp_duplicate =
true;
break;
}
}
return temp_duplicate;
}
// Print Array
public static void PrintArray(Array arr)
{
Console.Write ("{");
int count = 0;
int li = arr.Length;
foreach (object o in arr)
{
Console.Write("{0}", o);
count++;
//Condition to check whether ',' should be added in printing arrray
if (count < li)
Console.Write(", ");
}
Console.WriteLine ("}");
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

Hi I had totally forgot about this piece of code. Thanks for the reminder. To finish off the code add 'Console.ReadLine();' in the 'public static void PrintArray(Array arr)' e.g. public static void PrintArray(Array arr) { ..... Console.WriteLine("}"); Console.ReadLine(); } You will be able to see the random numbers generated in the console app. Cheers C

Posted by C B Dec 28, 2011

Just Delete the Return; and assign the value enterd into Intno variable And rest is history int intno; Console.WriteLine("Please enter a parameter eg. unique 5"); intno = Convert.ToInt32(Console.ReadLine());

Posted by Adarsh Acharya Dec 28, 2011

plz tell me this
this  program shows Please enter a parameter eg. unique 5" only
y the full program is nt execute plz tell how to execute this program....rpl on my id:-rohitnanda26@gmail.com....

waiting 4 rpl

Posted by rohit nanda Sep 19, 2010

Hello

Just started C# programming and wanted to create a Lottery Number generator.  Thought this code would help my development but it stops at:

                Console.WriteLine("Please enter a parameter eg. unique 5");
                return;

It does not allow me to enter a number.

What rookie mistake have I made. 

Cheers

C

Posted by C B Aug 19, 2010

tnx a lot ...

Posted by Mohamad Shiralizadeh Aug 12, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor