Technically what you referring are called pseudo-random numbers and not actual random numbers, BTW can we see what you have tried or instead you could find the complete details for your program here ?
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
You seem to have the big picture. Without knowing the specifics it appears that you are overwriting the boundaries of array called permutation. The values of i and j are to complicated to tell for sure. I'd suggest simplifying them. If permutations has N elements then the index of the elements ranges from 0 to N - 1. This is convenient because using rand() % N will give you random numbers ranging from 0 to N - 1. Therefore get a random number j = rand() % N, do your swap and repeat however many times you want, but start i at 0. Then, if you want the display to appear as originally posted make the adjustment at the time of display. That is:
for(int i = 0; i < N; ++i)
cout << i + 1 << " " << ++permutation[i].
How many times you get a random number j is up to you. If you don't do it more than N times you're not likely randomize things as well as you'd like because N repeats of j = rand() % N is likely to have several repeats of j for any value of N. I'd suggest getting j 2-3 times the value of N. Alternatively, if you use a second vector of ints with elements ranging from 0 to N- 1and remove elements as they are used in the real array/vector, then you can assure yourself that all indexes have been rearranged at least once when the vector.size() <= 1.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Hi thanks for the feedback but can u write a code for me for the output that i want. if you write code then i will ammend it according to my template. Actually i am working on one c++ template, so it would be easy for me if you provide me a simple code and then i will apply it on my template.
Why should someone else write your code for you ?
Lerner's already given you the actual ways you can go with the Logic, you just have to convert that in to your code.
As far as the way you should go, I suggest you follow the second method which Lerner suggested, hold a vector which contains the unused numbers in your range and remove each number once it gets used in your actual array.
Also to generate your number I suggest you use rand()%X , where X-> size of Vector; this would give you the number at which index in the Vector to use, and once you have used the element at that index, just "erase" it from the vector, As a result "X"(size of the vecor) would keep on decrementing and you will mostly not have to worry about rand() repeating values.
In case you want help with Vectors you can look here .
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
First: use code tags
next: void main
And then:
These lines:
for(i=0;i<N;i++)
i=1;
will cause an infinite loop. So your code won't work.
Also, if you want to make 'pairs of number', don't use an odd amount of elements in your array because 25/2 = 12.5...
I would set all the values of the int-array to a fixed number (-1 for example) to indicate that they didn't receive a random number yet. And then fill up the array with number until no value is -1 anymore
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403