-9

I know there are many questions on the net about it ,but I would like to know why my method fails What Am i Doing wrong?

public class Generator
{

    private static readonly Random random = new Random();

    private static readonly object SyncLock = new object();

    public static int GetRandomNumber(int min, int max)
    {
        lock (SyncLock)
        { 
            return random.Next(min, max);
        }
    }

}


[TestFixture]
public class Class1
{
    [Test]
    public void SimpleTest()
    {

        var numbers=new List<int>();
        for (int i = 1; i < 10000; i++)
        {
            var random = Generator.GetRandomNumber(1,10000);
            numbers.Add(random);
        }

        CollectionAssert.AllItemsAreUnique(numbers);

    }
}

EDIT Test method is failing!! Sorry for not mentioning

Thanks for your time and suggestions

12
  • 2
    @Boo i got scared when your comment popped up ... :O
    – Alex
    Mar 6, 2012 at 21:26
  • 3
    The recent influx of carelessly posed questions in enormous.
    – usr
    Mar 6, 2012 at 21:26
  • 1
    Are you looking for some sort of shuffling algorithm that will give you a random redistribution of the numbers 1-10000? Mar 6, 2012 at 21:32
  • 1
    @TimSchmelter: Whah? He posted code and said basically "it doesn't work." It's the awesomeness of the people that bothered to interpret and answer the "question" that makes the answers so good. It has zero to do with the quality of this question. Mar 6, 2012 at 21:43
  • 1
    @TimSchmelter: the OP claims that "the method fails" without specifying which method and in what manner it fails. If they had said that the test fails or the assert is tripped or something that accurately described the failure then I would agree with you. But there was no description of the failure beyond a "method", of which I count two that the OP owns and 6 that they do not own - any one of which could be "failing".
    – Sam Axe
    Mar 6, 2012 at 21:51

5 Answers 5

14

How can you possibly expect a sequence of 10,000 random numbers from a set of 10,000 possible values to be all unique unless you are extremely lucky? What you are expecting is wrong.

Flip a coin twice. Do you truly expect TH and HT to be the only possible sequences?

What makes you think random numbers should work any differently?

This output from a random number generator is possible:

1, 1, 1, 1, 1, 1, ..., 1

So is this:

1, 2, 3, 4, 5, 6, ..., 10000

In fact, both of those sequences are equally likely!

5
  • The row 1..10000 contains 10k unique numbers. Why is it impossible?
    – zerkms
    Mar 6, 2012 at 21:26
  • 9
    I said "unless you're extremely lucky."
    – jason
    Mar 6, 2012 at 21:28
  • 1
    @zerkms - Agreed. Not impossible. But you can be fairly confident that if you ran this program once a second for the lifetime of the universe that you'd never get a set without duplicates. This is a variant on the birthday problem: en.wikipedia.org/wiki/Birthday_problem Mar 6, 2012 at 21:30
  • @perfectionist, Jason: oops, I missed the "unless you're extremely lucky" part
    – zerkms
    Mar 6, 2012 at 21:32
  • @Jason .you are right I cannot go in all the details of the project but I need to produce unique numbers between 1 and 10000.
    – user9969
    Mar 6, 2012 at 22:33
2

You seem to be under the misapprehension that the Random class generates a sequence of unique, though apparently random numbers. This is simply not the case; randomness implies that the next number could be any of the possible choices, not just any except one I've seen before.

That being the case, it is entirely unsurprising that your test fails: the probability that 10000 randomly generated integers (between 1 and 10000 no less) are unique is minuscule.

0
2

Random != Unique

The point here is that your code should model your problem and yours really doesn't. Random is not equal to unique. If you want unique, you need to get your set of values and shuffle them.

If you truly want random numbers, you can't expect them to be unique. If your (P)RNG offers an even distribution, then over many trials you should see similar counts of every value (see the Law of Large Numbers). Cases may show up that seem "wrong" but you can't discount that you hit that case by chance.

3
  • -1 N unique numbers can be generated in O(n). OrderBy would run in O(n*log(n)) en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
    – L.B
    Mar 6, 2012 at 21:32
  • 1
    Guids are guaranteed to be unique, but there's no guarantee that they produce values sufficient for shuffling a sequence. If it's a shuffle that you want, use a shuffling algorithm.
    – jason
    Mar 6, 2012 at 21:35
  • 2
    It is ironic that your answer violates the principle stated in your first sentence. Jason is correct. Guids are a source of uniqueness, not a source of randomness. There is no guarantee whatsoever that you don't get ten thousand guids that are already sorted in order; in fact, there are some versions of the guid generation algorithm that do exactly that. Mar 6, 2012 at 21:40
0
public static void FisherYatesShuffle<T>(T[] array)
{
    Random r = new Random();
    for (int i = array.Length - 1; i > 0; i--)
    {
        int j = r.Next(0, i + 1);
        T temp = array[j];
        array[j] = array[i];
        array[i] = temp;
    }
}

int[] array = new int[10000];

for (int i = 0; i < array.Length; i++) array[i] = i;
FisherYatesShuffle(array);
1
  • @user231465 just be to able to shuffle any kind of array, not just integer arrays
    – L.B
    Mar 7, 2012 at 7:58
0

I think you failed to mention that your test method is failing.

It is failing because your random generator is not producing Unique numbers. I'm not sure how it would under it's current condition.

4
  • yes the test failed and I wanted it to pass to prove that I can generate random numbers Uniquely
    – user9969
    Mar 6, 2012 at 22:29
  • Then you need update your code to produce that result. As it stands now the failure of the test is a correct result. Mar 6, 2012 at 22:30
  • ok.I would like to update the code to produce the result but I cannot seem to find how?
    – user9969
    Mar 6, 2012 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.