6 Replies - 193 Views - Last Post: 06 February 2012 - 03:21 PM Rate Topic: -----

Topic Sponsor:

#1 Pillzan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 12

Unique number generator problem

Posted 06 February 2012 - 12:17 PM

Hello, im trying to insert 7 random values ranging from 1-35 into an array, but when i execute my code alot of zeros keeps popping up and im really stuck figuring out why that is.

Array = new int[7];
            Random rndnum = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < Array.Length; i++)
            {
                temp = rndnum.Next(1, 7);
                if (!Array.Contains(temp))
                {
                    Array[i] = temp;
                }
                temp = 0;

            }



Example of output: 2 4 1 0 5 0 0

Is This A Good Question/Topic? 0
  • +

Replies To: Unique number generator problem

#2 modi123_1  Icon User is offline

  • Suiter #2
  • member icon


Reputation: 3562
  • View blog
  • Posts: 14,989
  • Joined: 12-June 08

Re: Unique number generator problem

Posted 06 February 2012 - 12:22 PM

Did you take a gander at what that variant of 'next' is doing? The first parameter is the min value you want, and the second is the max. If you want a range of 1 to 35 perhaps use those?

http://msdn.microsof...y/2dx6wyd4.aspx
Was This Post Helpful? 1
  • +
  • -

#3 LaughingBelly  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 88
  • Joined: 11-April 11

Re: Unique number generator problem

Posted 06 February 2012 - 12:24 PM

Those zeros are the cases where you generated a duplicate number. Your if condition inside the loop checks to see if a duplicate has been generated. If not, it adds it to the array. But what happens when it is a duplicate - it just moves on to filling the next element in the array. It should go back and generate another number for the same index.

Also, you are generating numbers in the range of (1,7) instead of (1,35). If you are constrained to generate in the range of (1,7), you are probably better off initializing an array from 1 to 7 and picking numbers out of it at random. Its like picking a random number from a bag containing the numbers you want.
Was This Post Helpful? 2
  • +
  • -

#4 Pillzan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 12

Re: Unique number generator problem

Posted 06 February 2012 - 12:28 PM

oh yeah that (1,7) was a typo, just saw that now, sorry for that. I think i can solve it now, thanks for the input.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Unique number generator problem

Posted 06 February 2012 - 12:28 PM

Its pretty obvious that since you wanted 7 numbers you put 1-7 in your call to Random.

Any time you are using a new call you really should read about how to use it BEFORE using it. But honestly most of us don't.

When your assumptions about how it will work don't pan out and it behaves differently than you assumed, that's when reading the documentation is no longer optional.

MSDN page for Random


Finding answers to specific problems:
Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.
How to do a good search that will get you targeted answers.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Unique number generator problem

Posted 06 February 2012 - 12:40 PM

With only needing 35 unique numbers you can get away with the simple if array contains logic.

But you have to realize that as the size of the array grows, and as you fill a larger percentage of it, you will get more and more duplicates and it will take longer and longer to randomly get a number you haven't selected yet. This simple approach is unacceptable with large lists.

Here's a more efficient algorithm:

<loop>
  fill a sequencial array from 1 to x {35 in this case}
<loop>
  <loop>
     randomly select a number from 0 to sequenctialArrayLength-1
     take that element's value and add it to your random array
     remove that element from your sequencial array
  <loop>


So you start with
1
2
3
4
5
6
7

after randomly selecting 2 you have
seq[]    rnd[]
1         3
2
4
..



after randomly selecting 16 you have
seq()   rnd[]
1         3
2         16
4
..
12
13
15
16



As you select numbers you take them out of your pool of available numbers, and reduce the range of the Random accordingly. No duplicate selections. No exponential decrees in processing time as the loop progresses.
Was This Post Helpful? 1
  • +
  • -

#7 superkb10  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 230
  • Joined: 27-November 11

Re: Unique number generator problem

Posted 06 February 2012 - 03:21 PM

Okay, I noticed you don't have an else statement. What if Array.Contains(temp)is true? Remember, the default value for any int is 0. So if the array does contain temp, Array[i] won't have a value and will be set to 0.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1