Hello
I want to make pairs between random number and non random numbers. so that random number will come twice to its corresponding value. I think it is better to see the output so it will give the idea. Like the first column have the numbers from 1 to 25 and in the second column the numbers are generated randomly with in the range of 1 to 25. I want to generate random number so that they will come twice. Like if 16 is generated randomly at 1 then at 16th value of first column the random number should be 1. as I am new in c++ so please help in making pairs. I used seed function so that when I run it with different seed the program will give me different random numbers at every seed. Note that the first colum is not random but the second column is random.

1 16
2 5
3 8
4 6
5 2
6 4
7 13
8 3
9 11
10 18
11 9
12 12
13 7
14 24
15 22
16 1
17 21
18 10
19 23
20 25
21 21
22 15
23 19
24 14
25 20

Thanks

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 ?

i have a template which i cannot post. i am just posting the function that i made and i will call it later in the template.

void Permutation_Random(int N,int *permutation)
{
    long int i,j,temp,dummy;


i=1;while (i<N) {
    j=i+rand()%(N-i+1);
    temp=permutation[i];    
    permutation[i]=permutation[j];
    permutation[j]=temp;
temp="<<temp;cout<<"\n";
    i++;
    }
}

// i will call the function later in the program. i am using seed function//
//seed=9999;srand(seed);
//Permutation_Random(N,pm-1);
//k=0;while(k<N){cout<<k<<"-->"<<pm[k]<<" ";k++;cout<<"\n";}

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.

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.

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.

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.

ok i will make things simple and clear. following is the code. now plz help me in getting that output i want

void main ()
{
    long int i,j,k,temp,permutation[25],dummy;
    int N=25,seed=9999;
    srand(seed);
    for(i=0;i<N;i++)
    i=1;while (i<N){
    j=i+rand()%(N-i+1);
    cout<<i<<" "<<j<<"\n";

    permutation[i]=j;

    i++;    
    }

    i=1;while(i<N){cout<<permutation[i]<<" ";i++;}


}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.