Generating random numbers without repeating.C#
Check each number that you generate against the previous numbers:
List<int> listNumbers = new List<int>();
int number;
for (int i = 0; i < 6; i++)
{
do {
number = rand.Next(1, 49);
} while (listNumbers.Contains(number));
listNumbers.Add(number);
}
Another approach is to create a list of possible numbers, and remove numbers that you pick from the list:
List<int> possible = Enumerable.Range(1, 48).ToList();
List<int> listNumbers = new List<int>();
for (int i = 0; i < 6; i++)
{
int index = rand.Next(0, possible.Count);
listNumbers.Add(possible[index]);
possible.RemoveAt(index);
}
How can i generate random numbers without repeating/duplicate the same number?
First of make only one instance of Random
, and then use a list to store all generated numbers and check it before creating files with the newly generated number
Random rnd = new Random();
List<long> generatedNumbers = new List<long>();
for (int i = 0; i < numberoffiles; i++)
{
long rndsize = rnd.Next(1, 2000);
if(generatedNumbers.Contains(rndSize))
{
i--;
continue;
}
generatedNumbers.Add(rndSize);
FileStream fs = new FileStream(@"c:\temp\files\huge_dummy_file" + i, FileMode.CreateNew);
fs.Seek(rndsize * 1024, SeekOrigin.Begin);
fs.WriteByte(0);
fs.Close();
}
How can I create a List of random int numbers without repeating?
Or use LINQ. (By generating a sequence between Min and Max with Enumerable.Range
):
var rnd = new Random();
var res = Enumerable.Range(Min, Max - Min + 1).OrderBy(x => rnd.Next()).ToList();
And if you want to pick specific number of the sequence you can use Take
method. Like this:
var res = Enumerable.Range(Min, Max - Min + 1).OrderBy(x => rnd.Next()).Take(5).ToList();
Generating random numbers without repeating.C#
Check each number that you generate against the previous numbers:
List<int> listNumbers = new List<int>();
int number;
for (int i = 0; i < 6; i++)
{
do {
number = rand.Next(1, 49);
} while (listNumbers.Contains(number));
listNumbers.Add(number);
}
Another approach is to create a list of possible numbers, and remove numbers that you pick from the list:
List<int> possible = Enumerable.Range(1, 48).ToList();
List<int> listNumbers = new List<int>();
for (int i = 0; i < 6; i++)
{
int index = rand.Next(0, possible.Count);
listNumbers.Add(possible[index]);
possible.RemoveAt(index);
}
Generate non duplicate random number
You can use a recursive function to do this:
private static List<int> GetRandomRow(List<int> row, int colCount, int min, int max)
{
if(colCount <= 0)
{
return row;
}
var next = rnd.Next(min, max);
if(row.Contains(next))
{
return GetRandomRow(row, colCount, min, max);
}
row.Add(next);
return GetRandomRow(row, colCount - 1, min, max);
}
Then you can use your program like:
private static Random rnd = new Random();
static void Main(string[] args)
{
int min = 0; // minimum number
int max = 36; // maximum number
int rows = 10; // number of rows in my copun
int col = 7; // number of column in my copun
// Get the date of PC
string NameDate;
NameDate = DateTime.Now.ToString("yyyy.MM.dd");
string Week = "1-uge";
string ComName = "LYN LOTTO";
Random rnd = new Random();
Console.WriteLine("{0,22} \n {1,15} \n{2,18}", NameDate, Week, ComName);
for (int i = 0; i < rows; i++)
{
Console.Write($"{i + 1}.");
var row = new List<int>();
var currentRow = GetRandomRow(row, col, min, max);
foreach (var currentCol in currentRow)
{
Console.Write("{1,4}", i, currentCol);
}
Console.WriteLine();
}
Console.WriteLine("***** JOKER TAL *****");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < col; j++)
Console.Write("{0,4}", rnd.Next(1, 9));
Console.WriteLine();
}
}
Related Topics
How to Set a Proxy for Webbrowser Control Without Effecting the System/Ie Proxy
List Array Duplicates with Count
The 'Await' Operator Can Only Be Used Within an Async Lambda Expression
Is Deferred Execution in ASP.NET MVC View a Very Bad Thing
Regular Expression to Get the Src of Images in C#
What Does "Displayclass" Name Mean When Calling Lambda
How to Implement One "Catch'Em All" Exception Handler with Resume
Formatting Literal Parameters of a C# Code Snippet
Reading Fromuri and Frombody at the Same Time
.Net: Unable to Cast Object to Interface It Implements
Outofmemoryexception on Declaration of Large Array
Improving/Fixing a Regex for C Style Block Comments
How to Get the Name of Color While Having Its Rgb Value in C#
How to Know the Repeating Decimal in a Fraction
How to Set Chrome Preferences Using Selenium Webdriver .Net Binding
Create Http Post Request and Receive Response Using C# Console Application