I want to generate 4 digit random number and put it in the text box as serial number of something . how do I do that and yet to some extent be sure I wouldn't get duplicated numbers?
If you want to be sure that you don't get duplicates use a Guid:
Guid guid = Guid.NewGuid(); // however a Guid is not a 4 digit integer
If you want a random number use Random:
Random rnd = new Random();
int randomInt = rnd.Next(1000, 10000)
But note that you should not create the random instance in a loop because it is seeded with the current time. Otherwise you get repeating values. So either pas the random instance as parameter to this method, use a field/property or use the same instance in a loop which was created outside.
The easiest approach to get unique random numbers is to create new numbers if one already exists.
List<int> used = new List<int>();
Random random = new Random();
foreach(thing you want to do)
{
int current = random.Next(1000, 9999);
while(used.Contains(current))
current = random.Next(1000, 9999);
//do something
used.Add(current);
}
or some similar variation on this to meet your needs
Best way to do it is with the Random class:
Random random = new Random();
int result = random.Next(1000, 10000);
You could also use the RNGCryptoServiceProvider class, which gives you more secure random numbers:
RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();
byte[] bytes = new byte[4];
csp.GetBytes(bytes);
int yourNum = bytes.Select(x => x % 10).Aggregate(0, (a, b) => (a * 10) + b);
The Random class is used to create random numbers. (Pseudo-random that is of course.)
Example:
Random rnd = new Random();
int num = rnd.Next(1000, 10000); //creates number from 1000 to 9999
You can use HashSet for store generated number and check duplicate
The easiest way to do this is generate a list of all possible choices in order and shuffle it. Then after shuffling the list just go through the list "in order" and the sequence will be random and non repeating.
List<int> numbers = new List<int>(Enumerable<int>.Range(0,10000);
HelperFunctions.Shuffle(numbers); //There are plenty of examples of how to shuffle the list on this site.
foreach(var number in numbers)
{
Console.WriteLine(number.ToString("D4"); //Displays random numbers from 0000 to 9999 but never repeats
}
Like this:
var random = new Random();
var numbers = new List<int>(0);
for (var i = 0; i < 4; i++)
{
var number = random.Next(1000, 10000);
if (numbers.Contains(number))
{
i--;
continue;
}
numbers.Add(number);
}
tbx1.Text = numbers[0].ToString();
tbx2.Text = numbers[1].ToString();
tbx3.Text = numbers[2].ToString();
tbx4.Text = numbers[3].ToString();
This will give you a 4 digit random number.
Random random= new Random();
int RnNum = random.Next(1000,9999);
If you want to be sure that it doesn't get duplicated, then store the generated random values in Session & compare if its already generated. Of course there are other ways, if you don't want to do it the session way, write to a text file or something
You need to keep a record yourself of the numbers that have been generated in Dictionary or Hashset. Like
HashSet<int> generatedValues = new HashSet<int>();
Random rnd = new Random();
int randomInt = rnd.Next(1000,9999);
while(generatedValues.Contains(randomInt))
{
randomInt = rnd.Next(1000,9999);
}
generatedValues.Add(randomint);
Related
I have created a short program that creates 3 random integers between 1-9 and stores them in an array, however, I would not like any of them to repeat, that is, I would like each to be unique. Is there an easier way to generate 3 unique integers other than having to iterate through the array and comparing each integer to each other? That just seems so tedious if I were to increase my array to beyond 3 integers.
This is my code to generate 3 random numbers. I saw other code in Java, but I thought maybe C# has a easier and more efficient way to do it.
var number = new Numbers[3];
Random r = new Random();
for ( int i = 0; i < number.Length; i++)
{
number[i] = new Numbers(r.Next(1,9));
}
Console.WriteLine("The Three Random Numbers Are:");
foreach(Numbers num in number)
{
Console.WriteLine("{0}", num.Number);
}
I would do something like this:
var range = Enumerable.Range(1, 8);
var rnd = new Random();
var listInts = range.OrderBy(i => rnd.Next()).Take(3).ToList();
You could make an array or a list of the numbers that might be generated, e.g. 0, 1, 2, 3. Then you generate a number from 0 to this list's length, e.g. 2 and pick list[2] so for the next time you only have 0, 1, 3 in your list.
It takes longer to generate it, especially for long lists but it doesn't repeat numbers.
using System;
using System.Collections.Generic;
public class Test
{
static Random random = new Random();
public static List<int> GenerateRandom(int count)
{
// generate count random values.
HashSet<int> candidates = new HashSet<int>();
// top will overflow to Int32.MinValue at the end of the loop
for (Int32 top = Int32.MaxValue - count + 1; top > 0; top++)
{
// May strike a duplicate.
if (!candidates.Add(random.Next(top))) {
candidates.Add(top);
}
}
// load them in to a list.
List<int> result = new List<int>();
result.AddRange(candidates);
// shuffle the results:
int i = result.Count;
while (i > 1)
{
i--;
int k = random.Next(i + 1);
int value = result[k];
result[k] = result[i];
result[i] = value;
}
return result;
}
public static void Main()
{
List<int> vals = GenerateRandom(10);
Console.WriteLine("Result: " + vals.Count);
vals.ForEach(Console.WriteLine);
}
}
Grate explanation and answers from here
Source http://ideone.com/Zjpzdh
How do I store a random number into my array, but only if there is not a duplicate already inside the array? My code below still inputs a duplicate number.
Random rand = new Random();
int[] lotto = new int [6];
for (int i = 0; i < lotto.Length; i++)
{
int temp = rand.Next(1, 10);
while (!(lotto.Contains(temp)))//While my lotto array doesn't contain a duplicate
{
lotto[i] = rand.Next(1, 10);//Add a new number into the array
}
Console.WriteLine(lotto[i]+1);
}
Try this:
Random rand = new Random();
int[] lotto = new int[6];
for (int i = 0; i < lotto.Length; i++)
{
int temp = rand.Next(1, 10);
// Loop until array doesn't contain temp
while (lotto.Contains(temp))
{
temp = rand.Next(1, 10);
}
lotto[i] = temp;
Console.WriteLine(lotto[i] + 1);
}
This way the code keeps generating a number until it finds one that isn't in the array, assigns it and moves on.
There are a lot of ways to 'shuffle' an array, but hopefully this clears up the issue you were having with your code.
What you really want is to shuffle the numbers from 1 to 9 (at least that's what your example is implying) and then take the first 6 elements. Checking for duplicates is adding unnecessary indeterminism and really is not needed if you have a shuffle.
E.g take this accepted answer for a Fisher-Yates shuffle and then take the first 6 elements for lotto.
This would then look like this:
lotto = Enumerable.Range(1,9)
.Shuffle()
.Take(6)
.ToArray();
This question already has answers here:
Random.Next returns always the same values [duplicate]
(4 answers)
Closed 9 years ago.
I am trying to create a range of non duplicate random numbers between 1 - 10, I planned on doing this by storing each random number I made in to an array and then checking that array every time to make sure I ain't already used the number.
My problem is that instead of creating different random numbers such as 1, 2, 3 I just keep getting the same random number over and over.
randomNumber();
Label1.Text = randomRow + "";
randomNumber();
Label2.Text = randomRow + "";
randomNumber();
Label3.Text = randomRow + "";
public int randomNumber()
{
List<int> numbers = new List<int>();
int num = 0;
Random randNum = new Random();
num = randNum.Next(1, 11);
if (numbers.Contains(num))
{
num = randNum.Next(1, 11);
}
else
{
randomRow = num;
numbers.Add(num);
}
return randomRow;
}
Problem : everytime you are creating the RandomNumber object in too close time.
When you create a Random object, it's seeded with a value from the system clock. If you create Random instances too close in time, they will all be seeded with the same random sequence.
From Here
When you create a Random object, it's seeded with a value from the
system clock. If you create Random instances too close in time, they
will all be seeded with the same random sequence.
Solution :
move Random randNum = new Random(); outside the function randomNumber().
Try This:
Random randNum = new Random();
public int randomNumber()
{
List<int> numbers = new List<int>();
int num = 0;
num = randNum.Next(1, 11);
if (numbers.Contains(num))
{
num = randNum.Next(1, 11);
}
else
{
randomRow = num;
numbers.Add(num);
}
return randomRow;
}
My best gues is that you are using this in a loop. In this case because you declare
Random randNum = new Random();
evry time this will generate tha same number. Just declare it BEFORE the loop and it should be fine.
Also you should consider a different approch because it is not a good practice. Like:
int[] array = {1,2,3,4,5,6,7,8,9,10};
Random randNum = new Random();
int rand=0;
int temp;
for(int i = 0; i<10;i++)
{
rand = randNum.next(1,10-i);
temp=array[rand];
array[rand]=array[9-i];
array[9-i]=temp;
}
I'm trying to create a binary search tree with unique random numbers. I'm using SortedSet to represent my tree and then I make it into an array and then I use Contains to see if a certain number is in the tree. My problem is that I can't figure out how to get all the random numbers different in a simple way. I used the methods Unik and Nålen_Unik but in this code it only generates 1 number to the array
Random random = new Random();
Program Tal = new Program();
string nål = Tal.Nålen_Unik();
string TalIArray = Tal.Unik();
bool found = false;
SortedSet<string> Tree = new SortedSet<string>();
for (int x = 0; x < 50000; x++)
{
Tree.Add(TalIArray);
}
int y = 0;
string[] TreeArray = Tree.ToArray<string>();
while (y < TreeArray.Length)
{
Console.WriteLine(TreeArray[y]);
y = y + 1;
}
private string Unik()
{
int maxSize = 4;
char[] chars = new char[10000];
string a;
a = "0123456789";
chars = a.ToCharArray();
int size = maxSize;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = maxSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
}
private string Nålen_Unik()
{
int maxSize = 1;
char[] chars = new char[62];
string a;
a = "0123456789";
chars = a.ToCharArray();
int size = maxSize;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = maxSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
There are mainly three approaches that are used to get random numbers without collisions:
Keep all numbers that you have picked, so that you can check a new number against all previous.
Create a range of unique numbers, shuffle them, and pick one at a time from the result.
Create such a huge random number that the risk of collisions is so small that it's negligible.
The second approach is the same principle as shuffling a deck of cards. The third approach is used when creating a GUID, which is basically a random 128 bit value.
You could use the Random class and a HashSet which will guarantee no duplicate items.
The HashSet class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
E.g:
HashSet<int> t = new HashSet<int>();
Random r = new Random();
while(t.Count < 50)//or the desired length of 't'
{
t.Add(r.Next(0,1000));//adjust min/max as needed
}
foreach (int i in t)
{
Console.WriteLine(i);
}
Console.Read();
Will give you a collection of 50 guaranteed unique random integers.
Since the number of elements in the set is not a requirement for this question it seems irrelevant to even mention, though if this is a requirement you can simply modify the line t.Count < ? to get a set of a desired length.
You could use Guid.NewGuid() or new Random().Next()
Assuming that you want unique numbers in a limited range, one (simple but possibly inefficient) way to do it is to create a list of all possible values (say, 0-99). Then you use System.Random to pick a random between 0 and (number of elements in list - 1). Get that index from the list, output it and remove the element. If you repeat the process, that element can no longer be generated and the numbers will be unique until you've used all possible values.
Create one instance of Random class. Make sure it is one!
then use this code
private Random random = new Random();
random.Next();
I am trying to use random function in C# to randomly pick four from an array addetailsID which has over six elements.
I am placing these randomly picked into an another array strAdDetailsID:
string[] strAdDetailsID = new string[4];
for (int i = 0; i < 4; i++)
{
Random random = new Random();
int index = random.Next(0, addetailsID.Length);
value = addetailsID[index].ToString();
strAdDetailsID[i] = value;
}
Sometimes, I get two of the same values from the six elements. How can I get all four unique values to be picked?
You might be better off shuffling the array, and then choosing the first 4 elements.
You could do it with LINQ with this method.
List<string> list = new List<string>() { "There", "Are", "Many", "Elements", "To", "Arrays" };
foreach (var item in list.OrderBy(f => Guid.NewGuid()).Distinct().Take(4))
{
Console.WriteLine(item);
}
You have an issue with your placement of Random random ... but I believe you're attacking this the wrong way.
This could be solved by randomly ordering the source and taking the first 4 items.
var result = addetails.OrderBy(x => Guid.NewGuid()).Take(4).ToArray();
Assuming the contents of addetails are unique (like you imply), you will always get 4 unique values here. Using random correctly, it's still possible to get repeats (because it's random).
You need to generate 4 unique indices first, then pull the random values from the source array:
string[] addetailsID = new string[20]; // this is the array I want to index into
// generate the 4 unique indices into addetailsID
Random random = new Random();
List<int> indices = new List<int>();
while (indices.Count < 4)
{
int index = random.Next(0, addetailsID.Length);
if (indices.Count == 0 || !indices.Contains(index))
{
indices.Add(index);
}
}
// now get the 4 random values of interest
string[] strAdDetailsID = new string[4];
for (int i = 0; i < indices.Count; i++)
{
int randomIndex = indices[i];
strAdDetailsID[i] = addetailsID[randomIndex];
}
Put them in a list and remove the selected element from the list once it has been chosen.
The following algorithm works pretty well, and doesn't require extra storage or pre-shuffling. It does change the order of the source array, so if that's not feasible, then the pre-shuffling approach is best.
In pseudo-code:
result = []
For i = 0 to numItemsRequired:
randomIndex = random number between i and source.length - 1
result.add(source[randomIndex])
swap(source[randomIndex], source[i])
In C#:
string[] strAdDetailsID = new string[4];
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
int randIndex = rand.Next(i, adDetailsID.Length);
strAddDetails[i] = adDetailsID[randIndex];
string temp = adDetailsID[randIndex];
adDetailsID[randIndex] = adDetailsID[i];
adDetails[i] = temp;
}
Use a list instead and drop every item that is already used:
List<string> addetailsID = new List<string>{"1","2","3","4","5","6"};
string[] strAdDetailsID = new string[4];
Random random = new Random();
for (int i = 0; i < 4; i++)
{
int index = random.Next(0, addetailsID.Count);
string value = addetailsID[index].ToString();
addetailsID.RemoveAt(index);
strAdDetailsID[i] = value;
}
strAdDetailsID.Dump();
Well, you should select random items one by one, not considering the already-picked items when selecting the next one. This must be faster than shuffling.
If the source list is small, you can just make a copy and remove the chosen items from it. Otherwise, you go like this:
(let n be the number of items in your array)
let S be the set of chosen indices; S = empty at the beginning
choose a random index i from 0 to n-1-size(S)
for each index from S that is smaller than i (starting from the smallest index!), add one to i
now i is a chosen index, add it to S.
return to step 2 until your set contains 4 elements.