Prev: How can I add a color object when I use the PieChartElement class
Next: my DrawImage doesn't draw when addes to the c-tor
From: Harlan Messinger on 15 Jun 2010 15:27 Soren 'theDiver' Reinke wrote: > 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. 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.
From: kndg on 15 Jun 2010 22:48 On 6/15/2010 6:10 PM, Luigi 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 > 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.
From: Luigi on 16 Jun 2010 04:52 Thanks Fred. Luigi "Fred Mellender" wrote: > 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: Göran Andersson on 17 Jun 2010 03:26 On 2010-06-15 12:10, Luigi 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 > 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 -- Göran Andersson _____ http://www.guffa.com
From: Peter Duniho on 17 Jun 2010 11:29
Göran Andersson wrote: > On 2010-06-15 12:10, Luigi 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 >> > > You can loop from the lower bound and up, and calculate the probability > for each number to be picked: But that only works if the order doesn't need to be random. Which, when dealing with randomized data is not usually the case. To get the order random, you still wind up needing to shuffle the result. So you might as well just shuffle the entire selection range and then just pick the first N elements of the shuffled range. Performance-wise, if there is a very large disparity between the range of possible numbers and the actual number of numbers one needs, there might be some benefit to the "pick first, shuffle later" approach, because the memory required would only be for the number of numbers picked, rather than the entire selection range. But you'd have to have a pretty large selection range and a pretty small output range to make a two-pass algorithm worth bothering with. Otherwise, the memory performance won't wind up mattering at all, and the two-pass algorithm would actually be slower because it has more calls to the Random class, negating any potential performance benefit that might come from lower memory usage. Personally, I'd go with the simpler "shuffle and copy", and only look for alternatives if for some reason that proved to be too inefficient. And for sure, picking 10 random numbers from the range of 0 to 20 the "shuffle and copy" approach is not too inefficient. :) Pete |