Loading ...

How to generate set of 10 unique random numbers | CodeAsp.Net

How to generate set of 10 unique random numbers

 /5
0 (0votes)

Today one of my colleague asked me a simple question that how I can generate set of 10 unique random numbers. First I thought it is very simple but later I found it’s very logical. So I thought I should write a blog on this.

Here is my code -

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RandomNumber : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GenrateUniqueNumber();
}

private void GenrateUniqueNumber()
{
// object of an Array class.
int[] arr = new int[10];

// A boolean variable.
bool flag;

// First I am generating a random number, then I am checking it from array's value.
// If random number doesn’t exist in array, it will store in array.
for (int i = 0; i < 10; i++)
{
// Generate a random number and insert in randomNo variable.
// 1 is min value and 20 is max value, so random number generates only 1 to 20.
Random random = new Random();
int randomNo = random.Next(1, 20);

flag = true;
// In this for loop I am doing compression with each index value of array.
// If randomNo value and array index value is equal, then it will break and random number
// generate again.
for (int x = 0; x < arr.Length; x++)
{
if (randomNo == arr[x])
{

i--;
flag = false;
break;
}

}
// if randomNo value and array index value is not equal, then random number store in array.
if (flag)
arr[i] = randomNo;
}
Response.Write("Set of Ten Unique Random Numbers : </br>");
// In this for loop I am showing set of 10 unique random numbers.
for (int z = 0; z < arr.Length; z++)
{
Response.Write(arr[z] + "</br>");
}

}
}

Cheers!!
Happy Coding:)

Comments (6)

   
shaitender
try this ,You can do using collection also. private string numbers() { var numbers = new Collection(); var rnd = new Random(); for (int i = 0; i < 10; i++) { var num= rnd.Next(1, 12); if (!numbers.Contains(num)) numbers.Add(num); else i--; } var sb = new StringBuilder(); int counter = 1; foreach (var i in numbers) { sb.AppendFormat("{0}.{1} ",counter++, i); } return sb.ToString(); } Thanks
7/9/2010
 · 
by
   
hajan
Nice post.Shaitender's code seems quite more optimized .
7/9/2010
 · 
by
   
mohit
what about my Code Hajan
7/9/2010
 · 
by
   
hajan
'Nice post' was intended for your post and code
7/9/2010
 · 
by
   
mohit
My pleasure
7/10/2010
 · 
by
   
vijjendra
Hey, you also achive this as following ways:  var rand = new Random();
        var randomNumberList = new Collection<int>);
        //Check if the Collection  contains less than 10 numbers
        while (randomNumberList.Count() < 10)
        {
            Repeat:
            int num = rand.Next(21);
            //If Number does not exist, add the number to the collection
            if (!randomNumberList.Contains(num) && num != 0)
                randomNumberList.Add(num);

            else //If number exists, fetch a different number
                goto Repeat;
        }
        foreach (var i in randomNumberList)
        {
            Response.Write(i + " ,");
        }
7/10/2010
 · 
by

Top Posts