Re: Random numbers with no duplicates
From: Aggro (spammerdream_at_yahoo.com)
Date: 09/25/04
- Next message: Sonia: "Re: Random numbers with no duplicates"
- Previous message: Sonia: "Re: Random numbers with no duplicates"
- In reply to: Gary Labowitz: "Re: Random numbers with no duplicates"
- Next in thread: Sonia: "Re: Random numbers with no duplicates"
- Reply: Sonia: "Re: Random numbers with no duplicates"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Sonia: "Re: Random numbers with no duplicates"
- Previous message: Sonia: "Re: Random numbers with no duplicates"
- In reply to: Gary Labowitz: "Re: Random numbers with no duplicates"
- Next in thread: Sonia: "Re: Random numbers with no duplicates"
- Reply: Sonia: "Re: Random numbers with no duplicates"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|