Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Loop Inside Of Method (Not What You Think)


  • Please log in to reply
2 replies to this topic

#1 TomTheBeast


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:Java, C++, C#, PHP, JavaScript
  • Learning:C++, C#, PHP

Posted 19 June 2012 - 09:18 PM

Hey, well I'm wondering how I could use Random to recieve a random numer of a list and make sure it isnt used.
For example, I have a list called NumsToUse, which contains all digits from 0 to 9. I have to take the digits from that, and randomly place them in a string without there being any repeats. I'm not sure how i would do this, but my strcutre of it would be something like:
public List<int> NumsToUse = new List<int>();
public string getNums() {
string thestoof;
Random rnd = new Random();
int next = rnd.Next(0,9);
if (NumsToUse.Contains(next)) {
thestoof = (thestoof + ", " + next);
return thestoof;

But then how would I make it so when it checks if it contains the number, that isnt being used, to when it IS being used to redo the check?

I've figured out how i could do this, via recurison, and I've tried this method and now i get a Stack Overflow Exeception:

public int ifNum()
				int next = num.Next(0, 9);
				if (!(Nums.Contains(next)))
					return next;
				return ifNum();

		public void Randomize()
			foreach (Control button in panel1.Controls)
				if (button.GetType() == typeof(Button))
					int newNum = ifNum();
					button.Text = newNum.ToString();

  • 0

#2 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 June 2012 - 09:38 PM

You can use the following method to do your job. The method GetUniqeNumbers returns a string of length 10 which contains all the numbers 0-9 exactly once and the numbers will be random.
public static String GetUniqeNumbers()
	String numbers = String.Empty;
	Random rnd = new Random(5);
	while (numbers.Length < 9) {
		int number = rnd.Next(0, 9);
		if (!numbers.Contains(number.ToString())) {
			numbers += number.ToString();

	for (int number = 0; number <= 9; number++) {
		if (!numbers.Contains(number.ToString())) {
			numbers += number.ToString();

	return numbers;

  • 0

#3 Momerath


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 282 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Perl, Visual Basic .NET, Pascal, Ada, Assembly, Fortran, Scheme
  • Learning:Others

Posted 20 June 2012 - 04:52 AM

You'd want to use a Fisher-Yates (Knuth) Shuffle. No expensive Contains searches each round, no hoping that the next random number isn't already used. Runs in linear time.
  • 0

Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download