10 random numbers from 0 to 20

  • Thread starter Thread starter Luigi
  • Start date Start date
L

Luigi

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

Thanks a lot.

Luigi
 
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 = adding;
else
{
i--;
continue;
}
}

Luigi
 
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 = adding;
}

but it seems that give me also the number zero.

Luigi
 
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 = 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
 
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;
}
 
Soren said:
If you don't want repetitions they are not real random numbers.

It isn't a set of 10 *mutually independent* random selections. It *is*
one random selection of 10, just as when you randomly select 10 people
out of an audience, you aren't selecting the same person twice.
 
Hello,
how can I write a method that returns me 10 random numbers from 0 to 20
(included), without repetitions?

Thanks a lot.

Luigi

Hi Luigi,

An example,

using System;
using System.Collections.Generic;
using System.Linq;

public static class EnumerableExtensions
{
private static readonly Random random = new Random();

public static IEnumerable<T> SelectRandomItem<T>(this IEnumerable<T>
source, int total)
{
if (source == null) yield break;

T[] data = source.ToArray();

if (total < 0 || total > data.Length)
{
throw new ArgumentException();
}

for (int i = 0; i < total; i++)
{
int index = random.Next(data.Length - i);

yield return data[index];

data[index] = data[data.Length - i - 1];
}
}
}

// Usage
public class MyClass
{
public static void Main(string[] args)
{
var data = Enumerable.Range(0, 21);

foreach (var item in data.SelectRandomItem(10))
{
Console.WriteLine(item);
}
}
}

Regards.
 
Thanks Fred.

Luigi

Fred Mellender said:
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 said:
Hello,
how can I write a method that returns me 10 random numbers from 0 to 20
(included), without repetitions?

Thanks a lot.

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

Thanks a lot.

Luigi

You can loop from the lower bound and up, and calculate the probability
for each number to be picked:

int num = 0; // 0 to 20 inclusive
int cnt = 21;
int left = 10; // how many to pick
Random rnd = new Random();
while (left > 0) {
if (rnd.Next(cnt) < left) {
Console.WriteLine(num);
left--;
}
num++;
cnt--;
}

Example:

1
5
6
7
8
11
14
17
18
19
 
Back
Top