From: AA2e72E on

I could say 10?20 but that would more than likely mean nothing to you.
Perhaps
http://stackoverflow.com/questions/254844/random-array-using-linq-and-c might
provide another insight.
From: Peter Duniho on
Luigi wrote:
> 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.

Your original post states you want to also include the number zero. Do
you or don't you want zero in the output?

The correct (most efficient) way to accomplish what you want to do is to
initialize an array of numbers that is exactly the range of numbers you
want. For example, if you really do want the numbers "from 0 to 20
inclusive", then you need 21 elements, initialized starting at 0 and
ending with 20.

Whatever the range, then the next step is to "shuffle" the array. There
have been a number of discussions in this newsgroup already, which you
can review using Google Groups, or you can just look up "shuffle"
algorithms on Wikipedia. The basic idea is to select a random element
from the array to swap into the current position of the array, starting
with the first element; with each iteration, increment the current
position, and select a random element only from the range of that
position through to the end of the array.

Pete
From: Peter Duniho on
Peter Duniho wrote:
> [...]
> Whatever the range, then the next step is to "shuffle" the array. There
> have been a number of discussions in this newsgroup already, which you
> can review using Google Groups, or you can just look up "shuffle"
> algorithms on Wikipedia. The basic idea is to select a random element
> from the array to swap into the current position of the array, starting
> with the first element; with each iteration, increment the current
> position, and select a random element only from the range of that
> position through to the end of the array.

Oh, and it should go without saying, but just in case:

If you only want 10 numbers from that range, then you simply choose the
first 10 from the shuffled array.

Pete
From: Fred Mellender on
Put the integers 0-20 in a list. Shuffle it. Take the first 10 from the
list. To shuffle a list (not tested):

public static List<T> shuffledList(List<T> listToShuffle, Random rand)
{
/*
* Make a new list of elements picked from listToShuffle
* in a random order.
*/

List<T> randList = new List<T>(listToShuffle);
for (int k = randList.Count-1; k >= 0; k--)
{
int randIndx = rand.Next(k);
T temp = randList[k];
randList[k] = randList[randIndx];
randList[randIndx] = temp;
}

return randList;
}

"Luigi" <Luigi(a)discussions.microsoft.com> wrote in message
news:FCD10C7D-3A0B-4909-8E05-84D257BE8206(a)microsoft.com...
> 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: rossum on
On Tue, 15 Jun 2010 03:10:16 -0700, Luigi
<Luigi(a)discussions.microsoft.com> wrote:

>Hello,
>how can I write a method that returns me 10 random numbers from 0 to 20
>(included), without repetitions?
>
>Thanks a lot.
>
>Luigi
As others have said, put the numbers you want into some container or
array and shuffle.

The standard shuffling algorithm is in Knuth, Algorithm 3.4.2 P. It
is covered on Wikipedia:


http://en.wikipedia.org/wiki/Fisher–Yates_shuffle#The_modern_algorithm

The article contains Java code which should be easily convertible to
C#.

rossum