From: Luigi on
Hello,
how can I write a method that returns me 10 random numbers from 0 to 20
(included), without repetitions?

Thanks a lot.

Luigi

From: Soren 'theDiver' Reinke on
On 15-06-2010 12:10, Luigi wrote:
> Hello,
> how can I write a method that returns me 10 random numbers from 0 to 20
> (included), without repetitions?

If you don't want repetitions they are not real random numbers.

You could get the number '15' 10 times in a row, it is possible, but
highly unlikely.


--
Med venlig hilsen / Best regards
Søren Reinke, IDCS #505926, TDI Trimix
www.Dykkeren.dk
Dive blog with a large gallery

From: Luigi on
Maybe this one is correct:

int[] result = new int[10];
Random random = new Random();
int adding;

for (var i = 0; i < 10; i++)
{
adding = random.Next(1, 20);
if (!result.Contains(adding) || !result.Contains(0))
result[i] = adding;
else
{
i--;
continue;
}
}

Luigi
From: Luigi on
I'm trying in this way:

int[] result = new int[10];
Random random = new Random();
int adding;

for (var i = 0; i < 10; i++)
{
adding = random.Next(1, 20);
if (!result.Contains(adding))
result[i] = adding;
}

but it seems that give me also the number zero.

Luigi
From: Matt on
On Jun 15, 7:03 am, Luigi <Lu...(a)discussions.microsoft.com> wrote:
> Maybe this one is correct:
>
> int[] result = new int[10];
> Random random = new Random();
> int adding;
>
> for (var i = 0; i < 10; i++)
> {
>       adding = random.Next(1, 20);
>       if (!result.Contains(adding) || !result.Contains(0))
>               result[i] = adding;
>        else
>        {
>               i--;
>               continue;
>         }
>
> }
>
> Luigi

The easiest way to do what you are trying to accomplish is to create
an array
of 20 elements, then select a random number in that range. Remove the
item
(i.e. shift them down one, so that you now have 19 elements). Find a
random
number in that range, remove it and so forth until you have the number
you want.

Matt