Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How stop duplicates wen randomizing from 0 to 10

Status
Not open for further replies.

DSummZZZ

Programmer
Oct 24, 2000
4,250
0
0
US
Create two arrays 10 elements each. One empty (dest), and one with 10 numbers (source).
Get a random number from 1 to 10 and grab the associated number from source, and place it in dest in element 1. Then remove that number from source by shifting all larger numbers down one row.
Now grab a random number from 1 to 9 and put it in dest (2), 1 through 8 into dest(3) and so on until you get to one remaining number, and place it in the final element of dest.
After you've used all the numbers, you can then populate your buttons.
Make sense?


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Thank you for your respond. This is the code i wrote according to your instruction, but it does not work.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static Random random = new Random();
public Form1()
{
InitializeComponent();
}

private void OneButton_Click(object sender, EventArgs e)
{
int[] dest = new int[10];
int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int randnumber1 = random.Next(0, 10);
for (int i = 0; i < 10; i++)
{
if (randnumber1 == source)
{
dest[1] = source;
source = source.Where(val => val != randnumber1).ToArray();

}
}

int randnumber2 = random.Next(0, 9);
for (int i = 0; i < 10; i++)
{
if (randnumber2 == source)
{
dest[2] = source;
source = source.Where(val => val != randnumber2).ToArray();
}
}

int randnumber3 = random.Next(0, 8);
for (int i = 0; i < 10; i++)
{
if (randnumber3 == source)
{
dest[3] = source;
source = source.Where(val => val != randnumber3).ToArray();

}
}

int randnumber4 = random.Next(0, 7);
for (int i = 0; i < 10; i++)
{
if (randnumber4 == source)
{
dest[4] = source;
source = source.Where(val => val != randnumber4).ToArray();

}
}

int randnumber5 = random.Next(0, 6);
for (int i = 0; i < 10; i++)
{
if (randnumber5 == source)
{
dest[5] = source;
source = source.Where(val => val != randnumber5).ToArray();

}
}

int randnumber6 = random.Next(0, 5);
for (int i = 0; i < 10; i++)
{
if (randnumber6 == source)
{
dest[6] = source;
source = source.Where(val => val != randnumber6).ToArray();

}
}
int randnumber7 = random.Next(0, 4);
for (int i = 0; i < 10; i++)
{
if (randnumber7 == source)
{
dest[7] = source;
source = source.Where(val => val != randnumber7).ToArray();

}
}
int randnumber8 = random.Next(0, 3);
for (int i = 0; i < 10; i++)
{
if (randnumber8 == source)
{
dest[8] = source;
source = source.Where(val => val != randnumber8).ToArray();
}
}

int randnumber9 = random.Next(0, 2);
for (int i = 0; i < 10; i++)
{
if (randnumber9 == source)
{
dest[9] = source;
source = source.Where(val => val != randnumber9).ToArray();

}
}

int randnumber10 = random.Next(0, 1);
for (int i = 0; i < 10; i++)
{
if (randnumber10 == source)
{
dest[10] = source;
source = source.Where(val => val != randnumber10).ToArray();
}
}

OneButton.Text = dest.ToString();
TwoButton.Text = dest.ToString();
ThreeButton.Text = dest.ToString();
FourButton.Text = dest.ToString();
FiveButton.Text = dest.ToString();
FiveButton.Text = dest.ToString();
SixButton.Text = dest.ToString();
SevenButton.Text = dest.ToString();
EightButton.Text = dest.ToString();
}


}
}
 
Code:
var random = new Random();
var set = new HashSet<int>();
while(set.Count < 10)
{
   var number = random.Next(0,9)
   set.Add(number);
}
return set;
will produce 10 random numbers 0-9. a Set is unique type of collection that will only allow exactly 1 unique instance (including null)

the same could be accomplished using a List, the only difference is you would need to check for the value before adding
Code:
var set = new List<int>();
while(set.Count < 10)
{
   var number = random.Next(0,9)
   if(set.Contains(number)) continue;

   set.Add(number);
}
return set;

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thank you Jason, your method make sense. But as i'm a beginner, i'm struggling to implement this method with others, help!
 
the code i provided will create 10 unique random numbers. all that's left is apply the random values to the buttons.
Code:
buttonone.Text = set[0].ToString();
buttontwo.Text = set[1].ToString();
....
buttonten.Text = set[9].ToString();

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Did you try this? Remember i want to randomize 10 numbers, and get unique numbers. Your respond is now indexing set, while it was not used as an array, that is not working
 
right, the code i provided will create 10 random numbers between 0-9. these values are put into a set. you can then use these random numbers however you want. for example, assigning them to the text of a button.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Set end up with System.Collections.Generic.Hashset.1[System.Int32] when i display it. I dont think you tried it. And Set[0] cant work
 
A couple of changes to make it work for you then...

I've modified Jason's code as it got itself into an infinite loop for me and never returned the set
Code:
var random = new Random();
            var set = new HashSet<int>();
            while (set.Count < 10)
            {
                var number = random.Next(0, [red]10[/red]);
                set.Add(number);
            }
And seems you don't seem to want to read around how to output an index value from a set, you do it using ElementAt(), e.g.
Code:
button1.Text = set.ElementAt(0).ToString();
button2.Text = set.ElementAt(1).ToString();

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
or convert the set to a list
Code:
var list = set.ToList();
list[0]
list[1]
...

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Must be a slow day on TT, I'm going to reply just to say:

Yep, that's another way to do it. And now Motlatjo should have a few ideas.

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
Now i c flames. When i want to display the randomized text on the Button in the textbox (Numberstextbox.Text = EightButton.Text), it displays the second randomised number.

And as i display the buttons text in the textbox, i need to check if dat number exist, if it does, i dont dispaly it in textbox. no duplicates numbers in textbox.
 
Here is the code i tried, not working


if (NumbersTextbox.Text == "")
{
NumbersTextbox.Text = ZeroButton.Text;

}
else
{
char nums = (char)NumbersTextbox.Text[10];
for (int i = 0; i < nums; i++)
{
char ButNum = (char)ZeroButton.Text[1];
if (ButNum != nums)
{
NumbersTextbox.Text += ZeroButton.Text;
}
else
{
MessageBox.Show("Exist");
}
}

}
 
I was trying to compare characters as in loading in the text box, when i find that the charater exist in the textbox, then i dont load it
 
Using the solution provided can (although unlikely) hang the program due to an infinite loop. Random (0-10) could loop a million times before returning a particular number, say 9 for example.

I think it would be better to fill a list with 1-10 and then randomly swap 2 elements N times. (Like a shuffle).
 
@PGO01: Like this?
Code:
List<int> nums = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Random r = new Random();
int i;
while (nums.Count > 0)
{
[tab]i = r.Next(0, nums.Count());
[tab]listBox1.Items.Add(nums[i].ToString());
[tab]nums.Remove(nums[i]);
}
Lodlaiden

You've got questions and source code. We want both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top