fill array with random but unique numbers in C# [duplicate] - c#

This question already has answers here:
Generate N random and unique numbers within a range
(7 answers)
Best way to randomize an array with .NET
(19 answers)
Closed 6 years ago.
Okay I know there are few posts like this but I need to use only loops (for,do,while) and if, else to fill array with random but unique numbers so how shall i edit this code
int[] x = new int[10];
Random r = new Random();
int i;
for (i = 0; i < x.Length; i++) {
x[i] = r.Next(10);
Console.WriteLine("x[{0}] = {1}", i, x[i]);
}

You could check if the newly generated number already exists in the array, if not then add it to the array, if yes then generate a new one.
For examle:
class Program
{
static void Main(string[] args)
{
int[] x = new int[10];
Random r = new Random();
int i;
for (i = 0; i < x.Length; i++)
{
var next = 0;
while (true)
{
next = r.Next(10);
if (!Contains(x, next)) break;
}
x[i] = next;
Console.WriteLine("x[{0}] = {1}", i, x[i]);
}
Console.ReadLine();
}
static bool Contains(int[] array, int value)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == value) return true;
}
return false;
}
}

Without checking that generated number is already presented in array you can't to solve this task.
So you must generate new number and before inserting it into array check for uniq.

Related

How can I fill array with unique random numbers with for-loop&if-statement?

I'm trying to fill one dimensional array with random BUT unique numbers (No single number should be same). As I guess I have a logical error in second for loop, but can't get it right.
P.S I'm not looking for a more "complex" solution - all I know at is this time is while,for,if.
P.P.S I know that it's a really beginner's problem and feel sorry for this kind of question.
int[] x = new int[10];
for (int i = 0; i < x.Length; i++)
{
x[i] = r.Next(9);
for (int j = 0; j <i; j++)
{
if (x[i] == x[j]) break;
}
}
for (int i = 0; i < x.Length; i++)
{
Console.WriteLine(x[i);
}
Here is a solution with your code.
int[] x = new int[10];
for (int i = 0; i < x.Length;)
{
bool stop = false;
x[i] = r.Next(9);
for (int j = 0; j <i; j++)
{
if (x[i] == x[j]) {
stop = true;
break;
}
}
if (!stop)
i++;
}
for (int i = 0; i < x.Length; i++)
{
Console.WriteLine(x[i]);
}
A simple trace of the posted code reveals some of the issues. To be specific, on the line…
if (x[i] == x[j]) break;
if the random number is “already” in the array, then simply breaking out of the j loop is going to SKIP the current i value into the x array. This means that whenever a duplicate is found, x[i] is going to be 0 (zero) the default value, then skipped.
The outer i loop is obviously looping through the x int array, this is pretty clear and looks ok. However, the second inner loop can’t really be a for loop… and here’s why… basically you need to find a random int, then loop through the existing ints to see if it already exists. Given this, in theory you could grab the same random number “many” times over before getting a unique one. Therefore, in this scenario… you really have NO idea how many times you will loop around before you find this unique number.
With that said, it may help to “break” your problem down. I am guessing a “method” that returns a “unique” int compared to the existing ints in the x array, may come in handy. Create an endless while loop, inside this loop, we would grab a random number, then loop through the “existing” ints. If the random number is not a duplicate, then we can simply return this value. This is all this method does and it may look something like below.
private static int GetNextInt(Random r, int[] x, int numberOfRandsFound) {
int currentRand;
bool itemAlreadyExist = false;
while (true) {
currentRand = r.Next(RandomNumberSize);
itemAlreadyExist = false;
for (int i = 0; i < numberOfRandsFound; i++) {
if (x[i] == currentRand) {
itemAlreadyExist = true;
break;
}
}
if (!itemAlreadyExist) {
return currentRand;
}
}
}
NOTE: Here would be a good time to describe a possible endless loop in this code…
Currently, the random numbers and the size of the array are the same, however, if the array size is “larger” than the random number spread, then the code above will NEVER exit. Example, if the current x array is set to size 11 and the random numbers is left at 10, then you will never be able to set the x[10] item since ALL possible random numbers are already used. I hope that makes sense.
Once we have the method above… the rest should be fairly straight forward.
static int DataSize;
static int RandomNumberSize;
static void Main(string[] args) {
Random random = new Random();
DataSize = 10;
RandomNumberSize = 10;
int numberOfRandsFound = 0;
int[] ArrayOfInts = new int[DataSize];
int currentRand;
for (int i = 0; i < ArrayOfInts.Length; i++) {
currentRand = GetNextInt(random, ArrayOfInts, numberOfRandsFound);
ArrayOfInts[i] = currentRand;
numberOfRandsFound++;
}
for (int i = 0; i < ArrayOfInts.Length; i++) {
Console.WriteLine(ArrayOfInts[i]);
}
Console.ReadKey();
}
Lastly as other have mentioned, this is much easier with a List<int>…
static int DataSize;
static int RandomNumberSize;
static void Main(string[] args) {
Random random = new Random();
DataSize = 10;
RandomNumberSize = 10;
List<int> listOfInts = new List<int>();
bool stillWorking = true;
int currentRand;
while (stillWorking) {
currentRand = random.Next(RandomNumberSize);
if (!listOfInts.Contains(currentRand)) {
listOfInts.Add(currentRand);
if (listOfInts.Count == DataSize)
stillWorking = false;
}
}
for (int i = 0; i < listOfInts.Count; i++) {
Console.WriteLine(i + " - " + listOfInts[i]);
}
Console.ReadKey();
}
Hope this helps ;-)
The typical solution is to generate the entire potential set in sequence (in this case an array with values from 0 to 9). Then shuffle the sequence.
private static Random rng = new Random();
public static void Shuffle(int[] items)
{
int n = list.Length;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
int temp = items[k];
items[k] = items[n];
items[n] = temp;
}
}
static void Main(string[] args)
{
int[] x = new int[10];
for(int i = 0; i<x.Length; i++)
{
x[i] = i;
}
Shuffle(x);
for(int i = 0; i < x.Length; i++)
{
Console.WritLine(x[i]);
}
}
//alternate version of Main()
static void Main(string[] args)
{
var x = Enumerable.Range(0,10).ToArray();
Shuffle(x);
Console.WriteLine(String.Join("\n", x));
}
You can simply do this:
private void AddUniqueNumber()
{
Random r = new Random();
List<int> uniqueList = new List<int>();
int num = 0, count = 10;
for (int i = 0; i < count; i++)
{
num = r.Next(count);
if (!uniqueList.Contains(num))
uniqueList.Add(num);
}
}
Or:
int[] x = new int[10];
Random r1 = new Random();
int num = 0;
for (int i = 0; i < x.Length; i++)
{
num = r1.Next(10);
x[num] = num;
}

Bucket Sort algorithm issues c# [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 4 years ago.
I am learning about sorting algorithms, and am currently at a loss with my first "Distribution Pass". As my notations describe, the first loop is where I sort each value into my bucket array based on the one's value. It works for every value except for the 9th index of my values array. The loop terminates before it gets to the 9th index. I've tried changing the conditions of the for loop, but that doesn't seem to work. Some help or hints as to what is happening would be much appreciated!
** Edit this is not the complete algorithm, it is only the first two steps. I am following the instructions from my c# book. The notes placed before each loop are the word for word instructions from the book. I don't want to continue with the sorting algorithm until I get the first loop working properly.
public static void Sort(int[] values)
{
int n = values.Length;
int[,] buckets = new int[n, n - 1];
int index = 0;
// Place each value of the one-dimensional array
// into a row of the bucket array, based on the
// value's "one's" (rightmost) digit.
// "Distribution Pass"
for (int i = 0; i < values.Length -1; ++i)
{
// Get the one's value of values[i]
int ones = (values[i] % 10);
// Place the value in the appropriate bucket
buckets[ones, i] = values[i];
}
// Loop through the bucket array row by row,
// and copy the values back to the original
// array.
// "Gathering Pass"
for (int r = 0; r < n; r++)
{
for (int c = 0; c < n - 1; c++)
{
try
{
if (buckets[r, c] != 0)
{
values[index] = buckets[r, c];
index++;
}
}
catch
{
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Random rand = new Random();
int[] randomArray = new int[10];
label1.Text = "Unsorted Array: ";
label2.Text = " Sorted Array: ";
for (int i = 0; i < randomArray.Length; i++)
{
randomArray[i] = rand.Next(10, 100);
label1.Text += randomArray[i] + " ";
}
Sort(randomArray);
label2.Text += string.Join(" ", randomArray);
}

C# sequence unhandled exception [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
//1. add 10 numbers in sequence , print only steam numbers.
int[] seq= new int[10];
int n = 0;
int[] seq2= new int[n];
for (int i = 0; i < seq.Length; i++)
{
Console.WriteLine("Add number ");
seq[i] = int.Parse(Console.ReadLine());
if (seq[i]%2==0)
{
seq2[n] = seq[i];
n++;
}
}
for (int i = 0; i < seq2.Length; i++)
{
Console.WriteLine(seq2[i]);
}
There is something wrong with sequence2 and the program isn't telling anything about it , can someone help ? it's not about the task i did it other way but i just want to understand what did i do wrong here .
You declared Seq2 array with length 0 in below shown part of your code. So it will always fail with Index was outside the bounds of the array exception when you do this seq2[n] = seq[i];.
int n = 0;
int[] seq2= new int[n];
Declare Seq2 as list instead. Like this..
var seq2= new List<int>();
and then do this..
seq2.Add(seq[i]);
Your final code will look like this..
int[] seq= new int[10];
var seq2= new List<int>();
for (int i = 0; i < seq.Length; i++)
{
Console.WriteLine("Add number ");
seq[i] = int.Parse(Console.ReadLine());
if (seq[i]%2==0)
{
seq2.Add(seq[i]);
}
}
for (int i = 0; i < seq2.Count(); i++)
{
Console.WriteLine(seq2[i]);
}
Since you don't know the number of elements in the second array, and C# doesn't have dynamic arrays (I think), just use a list instead:
int[] seq= new int[10];
int n = 0;
List<int> seq2= new List<int>;
for (int i = 0; i < seq.Length; i++)
{
Console.WriteLine("Add number ");
seq[i] = int.Parse(Console.ReadLine());
if (seq[i]%2==0)
{
seq2.Add(seq[i]);
n++;
}
}
for (int i = 0; i < seq2.Length - 1; i++)
{
Console.WriteLine(seq2[i]);
}
Are the arrays obligatory?
int n = 10;
for (int i = 0; i < n; i++)
{
Console.WriteLine("Add number ");
int a = int.Parse(Console.ReadLine());
if (a%2==0)
{
Console.WriteLine(a);
}
}
If they are, you will need a List because you don't know how many of them will be even.
Edit: just read the bottom line..

Generating random numbers without repeating.C# [duplicate]

This question already has answers here:
Random number generator with no duplicates
(12 answers)
Closed 2 years ago.
Hi everyone I am trying to generate 6 different numbers on the same line in c# but the problem that i face is some of the numbers are repeating on the same line.Here is my code to
var rand = new Random();
List<int> listNumbers = new List<int>();
int numbers = rand.Next(1,49);
for (int i= 0 ; i < 6 ;i++)
{
listNumbers.Add(numbers);
numbers = rand.Next(1,49);
}
somewhere my output is
17 23 23 31 33 48
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);
}
listNumbers.AddRange(Enumerable.Range(1, 48)
.OrderBy(i => rand.Next())
.Take(6))
Create a HashSet and generate a unique random numbers
public List<int> GetRandomNumber(int from,int to,int numberOfElement)
{
var random = new Random();
HashSet<int> numbers = new HashSet<int>();
while (numbers.Count < numberOfElement)
{
numbers.Add(random.Next(from, to));
}
return numbers.ToList();
}
Make it a while loop and add the integers to a hashset. Stop the loop when you have six integers.
Instead of using a List, you should use an HashSet. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, Please find the example code below.
public static IEnumerable<int> GetRandomNumbers(int count)
{
HashSet<int> randomNumbers = new HashSet<int>();
for (int i = 0; i < count; i++)
while (!randomNumbers.Add(random.Next()));
return randomNumbers;
}
I've switched your for loop with a do...while loop and set the stopping condition on the list count being smaller then 6.
This might not be the best solution but it's the closest to your original code.
List<int> listNumbers = new List<int>();
do
{
int numbers = rand.Next(1,49);
if(!listNumbers.Contains(number)) {
listNumbers.Add(numbers);
}
} while (listNumbers.Count < 6)
The best approach (CPU time-wise) for such tasks is creating an array of all possible numbers and taking 6 items from it while removing the item you just took from the array.
Example:
const int min = 1, max = 49;
List<int> listNumbers = new List<int>();
int[] numbers = new int[max - min + 1];
int i, len = max - min + 1, number;
for (i = min; i < max; i++) numbers[i - min] = i;
for (i = 0; i < 6; i++) {
number = rand.Next(0, len - 1);
listNumbers.Add(numbers[number]);
if (number != (len - 1)) numbers[number] = numbers[len - 1];
len--;
}
If you are not worried about the min, max, and range then you can use this.
var nexnumber = Guid.NewGuid().GetHashCode();
if (nexnumber < 0)
{
nexnumber *= -1;
}
What you do is to generate a random number each time in the loop. There is a chance of course that the next random number may be the same as the previous one. Just add one check that the current random number is not present in the sequence. You can use a while loop like: while (currentRandom not in listNumbers): generateNewRandomNumber
Paste the below in the class as a new method
public int randomNumber()
{
var random = new Random();
int randomNumber = random.Next(10000, 99999);
return randomNumber;
}
And use the below anywhere in the tests wherever required
var RandNum = randomNumber();
driver.FindElement(By.CssSelector("[class='test']")).SendKeys(**RandNum**);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] que = new int[6];
int x, y, z;
Random ran = new Random();
for ( x = 0; x < 6; x++)
{
que[x] = ran.Next(1,49);
for (y = x; y >= 0; y--)
{
if (x == y)
{
continue;
}
if (que[x] == que[y])
{
que[x] = ran.Next(1,49);
y = x;
}
}
}
listBox1.Items.Clear();
for (z = 0; z < 6; z++)
{
listBox1.Items.Add(que[z].ToString());
}
}
}

How to make this code work without repeating the numbers? [duplicate]

This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 9 years ago.
I need to print numbers from 1 to 50 in random order without repeating
it .
static void Main(string[] args)
{
ArrayList r = new ArrayList();
Random ran = new Random();
for (int i = 0; i < 50; i++)
{
r.Add(ran.Next(1,51));
}
for (int i = 0; i < 50; i++)
Console.WriteLine(r[i]);
Console.ReadKey();
}
What you want here is the Fisher Yates Shuffle
Here is the algorithm as implemented by Jeff Atwood
cards = Enumerable.Range(1, 50).ToList();
for (int i = cards.Count - 1; i > 0; i--)
{
int n = ran.Next(i + 1);
int temp = cards[i];
cards[i] = cards[n];
cards[n] = temp;
}
If you don't want to repeat the numbers between 1 and 50, your best bet is to populate a list with the numbers 1 to 50 and then shuffle the contents. There's a good post on shuffling here:
Randomize a List<T>
All you need to do is this check if the number already exists in the list and if so get another one:
static void Main(string[] args)
{
ArrayList r = new ArrayList();
Random ran = new Random();
int num = 0;
for (int i = 0; i < 50; i++)
{
do { num = ran.Next(1, 51); } while (r.Contains(num));
r.Add(num);
}
for (int i = 0; i < 50; i++)
Console.WriteLine(r[i]);
Console.ReadKey();
}
Edit: This will greatly increase the effeciency, preventing long pauses waiting for a non-collision number:
static void Main(string[] args)
{
List<int> numbers = new List<int>();
Random ran = new Random();
int number = 0;
int min = 1;
int max = 51;
for (int i = 0; i < 50; i++)
{
do
{
number = ran.Next(min, max);
}
while (numbers.Contains(number));
numbers.Add(number);
if (number == min) min++;
if (number == max - 1) max--;
}
for (int i = 0; i < 50; i++)
Console.WriteLine(numbers[i]);
Console.ReadKey();
}

Categories