| | |
Pairs in random numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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 ?
Last edited by stephen84s; Jun 28th, 2008 at 1:28 pm.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
•
•
Join Date: Jun 2008
Posts: 4
Reputation:
Solved Threads: 0
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";}
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";}
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
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.
•
•
•
•
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.
Last edited by stephen84s; Jun 29th, 2008 at 7:13 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
•
•
Join Date: Jun 2008
Posts: 4
Reputation:
Solved Threads: 0
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++;}
}
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:
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
next: void main
And then:
These lines:
cpp Syntax (Toggle Plain Text)
for(i=0;i<N;i++) i=1;
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
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Hello I am new here. can someone explain how to fix a segmentation fault (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- 2D array with uniqqe random numbers... (C++)
- Really stuck with this any help would be appreciated (C++)
- Shuffle with Random Generator (C++)
- Help! How to pull numbers from table at random (C++)
- how to eliminate all keyboard inputs accept a certain range (C)
- Argument Not Optional message in VB (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: warning C4715: 'Wagering::wageringTest' : not all control paths return a value
- Next Thread: Search for a word in a dictionary
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






