6 Replies - 593 Views - Last Post: 21 December 2009 - 12:50 AM Rate Topic: -----

#1 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Array problem

Posted 20 December 2009 - 03:57 PM

So i have this code, where im trying to generate random numbers into an array, but i dont want the numbers of the array to be equal, or repeat in other words ive tryed alot of things but i guess im not looking at this the correct way, there must be an easy way to do this. Im new to C# so i dont know all the methods yet.
private void btnStart_Click(object sender, EventArgs e)
		{
			Random ran = new Random();
			int[] randomNum = new int[6];
			for (int j = 0; j < 6; j++)
			{
				randomNum[j] = ran.Next(imageNameArray.Length);
			}
			picOne.Image = Image.FromFile(imageNameArray[randomNum[0]]);
			picTwo.Image = Image.FromFile(imageNameArray[randomNum[1]]);
			picThree.Image = Image.FromFile(imageNameArray[randomNum[2]]);
			picFour.Image = Image.FromFile(imageNameArray[randomNum[3]]);
			picFive.Image = Image.FromFile(imageNameArray[randomNum[4]]);
			picSix.Image = Image.FromFile(imageNameArray[randomNum[5]]);
		}


basicly when I hit the start button this code will run, what it has to do is create a 6 new slots in an array and then fill them with random numbers wich will be used to select six images from a file. Sometimes it gets reapeted images and i dont want that, the way to do according to me is by going throw the random numbers array and see that they have no reapeted ones.

But i fail at that, if anyone could help me i would greatly apreciate it.

This post has been edited by poncho4all: 20 December 2009 - 03:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array problem

#2 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Array problem

Posted 20 December 2009 - 04:37 PM

You could swap the items in imageNameArray first(randomly) and then load/use them in order of the array. That way you'll never get the same image more than once. It's the same kind of algorithm as what is used for shuffling a virtual deck of cards, search for that is my advice.
Was This Post Helpful? 0
  • +
  • -

#3 remorseless  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: Array problem

Posted 20 December 2009 - 04:55 PM

private void btnStart_Click(object sender, EventArgs e)
		{
			Random ran = new Random();
			int[] randomNum = new int[6];
			for (int j = 0; j < 6; j++)
			{
								int current_int = ran.Next(imageNameArray.Length);
								while(randomNum.contains(current_int))current_int= ran.Next(imageNameArray.Length);
				randomNum[j] = current_int;
			}
			picOne.Image = Image.FromFile(imageNameArray[randomNum[0]]);
			picTwo.Image = Image.FromFile(imageNameArray[randomNum[1]]);
			picThree.Image = Image.FromFile(imageNameArray[randomNum[2]]);
			picFour.Image = Image.FromFile(imageNameArray[randomNum[3]]);
			picFive.Image = Image.FromFile(imageNameArray[randomNum[4]]);
			picSix.Image = Image.FromFile(imageNameArray[randomNum[5]]);
		}



I haven't tested it, it's just on the spot coding, tell me if it works or not.
Was This Post Helpful? 1
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Array problem

Posted 20 December 2009 - 05:13 PM

so contain is a member of the array class, and it will go throw the array looking for similarities on the new number and each of the number in the array, right?
Was This Post Helpful? 0
  • +
  • -

#5 remorseless  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: Array problem

Posted 20 December 2009 - 05:59 PM

View Postponcho4all, on 21 Dec, 2009 - 09:13 AM, said:

so contain is a member of the array class, and it will go throw the array looking for similarities on the new number and each of the number in the array, right?


Yeah, It'll go through each of the elements in the array and look for similarities in what you entered. Again, make sure to check that it works because I'm not too positive that it works, I wrote it on the spot without checking. Hope I helped ;)
Was This Post Helpful? 0
  • +
  • -

#6 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Array problem

Posted 20 December 2009 - 07:02 PM

You sure did helped it works just fine, thanks much.
Was This Post Helpful? 0
  • +
  • -

#7 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Array problem

Posted 21 December 2009 - 12:50 AM

I think that a solution that Renagado proposed is really best one, because it's simple and gives you just the result you need.

Of course, generating random numbers and then getting image files is also a possibility, but is not real most optimal.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1