in reply to Re: Select three random numbers between 1..10
in thread Select three random numbers between 1..10
You have the write code to select a single random element from the array. To get unique random numbers, you need to remove that element from the array and then keep selecting from the shorter set.Luckely, you don't have to. While it hardly matters for an array of 10 elements, the principle doesn't scale. splice is an expensive operation, whose expected run time is Θ (N) for a single delete. If you have to select k elements from an array of size N, you have an expected running time of Θ (k * N). OTOH, if you perform a partial Fisher-Yates shuffle (you only need to get k elements), your running time reduces to O (k) (after setting up the array).
Abigail
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Select three random numbers between 1..10
by iburrell (Chaplain) on Mar 17, 2004 at 03:46 UTC |