Re: Random numbers with no duplicates

From: Aggro (spammerdream_at_yahoo.com)
Date: 09/25/04


Date: Sat, 25 Sep 2004 05:18:42 GMT

Gary Labowitz wrote:

> Generate the random numbers any way you know how within the range (probably
> rand() ), insert them into a set and check for whether or not the set
> accepted the insertion. When it does, add to a counter that controls the
> loop that generates the numbers and does the set insert. When the set
> doesn't accept the number, just repeat the loop.

This is easy way, but in theory this could cause the program to fall in
infinitive loop, because it could happen so that rand() returns you the
same number many times. In practise this causes the process of
generating random numbers to be slow. Good example would be if you have
to sort 1 000 000 numbers in random order. The loop would have to run
quite a while.

The way this is normally done is
- Generate array of numbers in order (for example {1,2,3,4,5,6})
- use rand() or similar to generate random number between 0 and amount
of numbers in array - 1 (0-5)
- Use the previous number as an index number to get number from your
array. For example, if you got 3 as an index, your first number is 4
- Add 4 to some array to hold called numbers, and remove it from the
original array, so that your original array would look like this {1,2,3,5,6}
- Generate random number between 0 and array size -1 (0-4 )
- Use this number as an index to get second number and again remove it.
Repeat untill you have the amount of numbers you want, or untill all
numbers have been called.

This way the loop will have to run only the same amount of times as how
many numbers you need.



Relevant Pages

  • Re: rs232...
    ... 'j' for non-equality to BYTESTOREAD in the end of 'do' loop, ... #define BYTESTOREAD 25 //Depicts the amount of bytes to read! ... //immediatelly assigned to this array. ... // from the rs232 port), ...
    (microsoft.public.vc.language)
  • Re: Random numbers with no duplicates
    ... >> loop that generates the numbers and does the set insert. ... because it could happen so that rand() returns you the ... > Repeat untill you have the amount of numbers you want, ... > This way the loop will have to run only the same amount of times as how ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Multi dimensional array
    ... > Then the user types in the amount he wants to order from that article. ... > and the amount in another, but then I have to loop through them and ... > other array - ... because the PHP statment <? ...
    (comp.lang.php)
  • Multiplying all results from a for loop
    ... I have a for loop that runs x amount of times, producing x amount of ... array. ... I want to then multiply all the arrays from the for loop by ...
    (comp.soft-sys.matlab)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)

Loading