| | |
Help needed filling array with unique random numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
Can't you just create an array of 5000 ints
Then shuffle them around. So you would be picking two random index positions and swapping them. Do this say a couple of hundred times.C++ Syntax (Toggle Plain Text)
for (int i =0; i< 5000; i++ ) { array[i] =i; }
Then just print out the first 200 elements and you will get 200 random elements with no repeats in a number range from 0 - 5000.
iamthwee makes a good point. The fix I provided will only be moderately effective at best, with decreasing effectiveness as you try to build a longer list (since you have to search the list each time and have more chances of a duplicate).
Also, neither of these will work if you were to build a list over 5000, for obvious reasons. And by disallowing duplicates, the array is technically not random
Also, neither of these will work if you were to build a list over 5000, for obvious reasons. And by disallowing duplicates, the array is technically not random
•
•
•
•
To fix that, you should put the random generator in a loop and use the LinearSearch results as a loop condition:
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < MAXSIZE; i++) { do { temp = rand() % 5000; // get random number } while (LinearSearch(list, temp) != -1); // repeat till it's unique list[i] = temp; // save it }
•
•
•
•
Originally Posted by ~s.o.s~
• The loop counter in the loop which generates random numbers should not be incremented if a duplicate in found in the list.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi vijayan121,
I think you may be able to help me, I haven't touched a line C in years, but the algorithm is what interests me, not the syntax. I guess the same must be doable in java.
Anyhow, I've got the same requirement, and my problem is the performance, it takes ages for my code to produce me my list.
I was suspecting that it was not the best solution to iterate over the list of elements to check if the random number was not taken yet and in that case regenerating a random number. From what I understand, your code should do the trick far quicker.
However, I am having some trouble understanding the logic. Could you pls elaborate? What does 'M' represent? What is obstream? Any chance you could give us more explanationsK? Thanks!
I think you may be able to help me, I haven't touched a line C in years, but the algorithm is what interests me, not the syntax. I guess the same must be doable in java.
Anyhow, I've got the same requirement, and my problem is the performance, it takes ages for my code to produce me my list.
I was suspecting that it was not the best solution to iterate over the list of elements to check if the random number was not taken yet and in that case regenerating a random number. From what I understand, your code should do the trick far quicker.
However, I am having some trouble understanding the logic. Could you pls elaborate? What does 'M' represent? What is obstream? Any chance you could give us more explanationsK? Thanks!
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> What does 'M' represent?
M is the cardinality of the set of numbers out of which we have to choose N *unique* numbers. (M>=N)
> Any chance you could give us more explanations?
a. probability that the first number is chosen is
ie.
here is a java transliteration of the alogorithm (caveat: java is a foreign language to me, there may be syntax errors. but the logic is sound).
M is the cardinality of the set of numbers out of which we have to choose N *unique* numbers. (M>=N)
> Any chance you could give us more explanations?
a. probability that the first number is chosen is
N/M b. probability that the second number is chosen is (N-1)/(M-1) if the first was chosen,N/(M-1) if it was not.ie.
N/M * (N-1)/(M-1) + (M-N)/M * N/(M-1) == N/M and so on.here is a java transliteration of the alogorithm (caveat: java is a foreign language to me, there may be syntax errors. but the logic is sound).
java Syntax (Toggle Plain Text)
public class unique_rand_generator { // fill up array with unique random integers // from the range min_value .. max_value // invariant: max_value - min_value + 1 (M) // >= array.length (N) public static void fill( int[] array, int min_value, int max_value ) { int available = max_value - min_value + 1 ; int required = array.length ; Random rng = new Random() ; for( int i=0 ; i<available ; ++i ) { // we have to choose required // out of the remaining (available-i) if( ( rng.nextInt(available-i) ) < required ) { --required ; array[required] = min_value + i ; } } } }
Last edited by vijayan121; Mar 14th, 2008 at 9:55 am.
My algorithm is a little bit different. First, I generate the random number into the array in order. Then randomly swap the array elements to make it disorder. For example: I want to generate 11 unique numbers from range of 1 to 1000. Firstly, I would fill my array with random number in order by create recursive function that firstly random a number into the middle of the array. Then, on the left side of that number I generate the number from 1-random_number and on the right side of that number I generate the number from random_number to max_number.
However, this algorithm has its weak point, what happen if the first number is 1, but we can easily solve that problem.
C++ Syntax (Toggle Plain Text)
___ ___ ___ ___ ___ ___ 320 ___ ___ ___ ___ ___ ___ ___ ___ 150 245 ___ ___ 16 85 270 290 ___ ___ 570 630 ___ ___ 421 524 724 815 16 85 150 245 270 290 320 421 424 570 630 724 815
However, this algorithm has its weak point, what happen if the first number is 1, but we can easily solve that problem.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: virtual class with friend functions
- Next Thread: Simple Varifacation please
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






