Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
How to generate Random numbers without repeating any numbers in the given target like(1-5000) there should be no reparation and I should be able to get 5000 randomized numbers. I should be able to run the randomized as many times as the user wants,How to do with C#?.
 
For Sample
Target is (1-10)
No of times to run =2
 
First run:
5,4,6,8,3,1,2,0,7,10
second run
6,3,5,8,4,2,0,1,10,7
 
Like this I should be able to get the results as many times as I run..
Posted 24 Aug '11 - 1:52
sacraj849


6 solutions

The first solution may work, but it will start to become slower the more numbers were already chosen. (after all, after having 4999 numbers selected there is only one possibility, but it may be generating any value, needing to try again, and again, and again).
 
One thing that may make it faster is using a HashSet instead a list, as searching in a HashSet is faster. But my real solution is different.
 
First we create a list which all possible values.
List<int> available = new List<int>(5000);
for(int i=1; i<=5000; i++)
  available.Add(i);
 
Then, we will keep generating random indexes (instead of random values). Such indexes will be used to get the value from the available list, put it in the result and then remove it from the available list.
List<int> result = new List<int>(5000);
while(available.Count > 0)
{
  int index = random.Next(availableCount);
  result.Add(available[index]);
  available.RemoveAt(index);
}
 
return result;
 
This way, if you create a list with 1 million values, you will have only 1 million calls to random.Next();
Surely the list itself has its own problems, but it is better than keep waiting until the last available number is guess.
  Permalink  
Comments
dr.m-rostami - 27 Sep '12 - 17:20
this is the best solution thx all
I believe the tip linked below covers what you are after.
 
How to generate many random various numbers?
 
I recommend looking at alternative 1
  Permalink  
See my tip: "Random extraction of 5 cards from a deck"[^], it is C++ but you should get the idea.
  Permalink  
Try this snippet
private static List<int> Randomize(int count, int min, int max)
        {
            Random r = new Random();
            List<int> result = new List<int>();
            if (count < max - min)
            {
                while (result.Count <= count)
                {
                    int number = r.Next(min, max);
                    if (!result.Contains(number))
                    {
                        result.Add(number);
                    }
                }
            }
            else
            {
                Console.WriteLine("Select another boundaries or number count");
            }
            return result;
        }
  Permalink  
  Permalink  
Comments
Simon Bang Terkildsen - 24 Aug '11 - 8:29
This doesn't solve the OP's problem.
i'm colecting all solution and this is the result:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
 
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        public static Random r=new Random();
        public static int number;
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
           //creat a text box in desing mode and call it ASPxTextBox1
            if (ASPxTextBox1.Text == "")
            {
                Response.Write("first enter you count number");
            }
            else
            {
                number = Convert.ToInt32(ASPxTextBox1.Text);
                List<int> available = new List<int>(number);
                for (int i = 1; i <= number; i++)
                    available.Add(i);
                List<int> result = new List<int>(number);
                while (available.Count > 0)
                {
                    int index = r.Next(available.Count);
                    result.Add(available[index]);
                    available.RemoveAt(index);
                }
                for (int i = 0; i < result.Count; i++)
                {
                    Response.Write(result[i] + "-");
                }
            }
        }
 
        protected int Pro_R(int min, int max)
        {
            return (r.Next(min, max));
        }
    }
}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 259
1 OriginalGriff 220
2 Mahesh Bailwal 159
3 CPallini 143
4 Maciej Los 140
0 Sergey Alexandrovich Kryukov 10,214
1 OriginalGriff 7,819
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid