Pairs in random numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 4
Reputation: abacian1983 is an unknown quantity at this point 
Solved Threads: 0
abacian1983 abacian1983 is offline Offline
Newbie Poster

Pairs in random numbers

 
0
  #1
Jun 28th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Pairs in random numbers

 
0
  #2
Jun 28th, 2008
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 ?"
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 4
Reputation: abacian1983 is an unknown quantity at this point 
Solved Threads: 0
abacian1983 abacian1983 is offline Offline
Newbie Poster

Re: Pairs in random numbers

 
0
  #3
Jun 28th, 2008
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";}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Pairs in random numbers

 
0
  #4
Jun 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 4
Reputation: abacian1983 is an unknown quantity at this point 
Solved Threads: 0
abacian1983 abacian1983 is offline Offline
Newbie Poster

Re: Pairs in random numbers

 
0
  #5
Jun 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Pairs in random numbers

 
0
  #6
Jun 29th, 2008
Originally Posted by abacian1983 View Post
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 ?"
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 4
Reputation: abacian1983 is an unknown quantity at this point 
Solved Threads: 0
abacian1983 abacian1983 is offline Offline
Newbie Poster

Re: Pairs in random numbers

 
0
  #7
Jun 30th, 2008
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++;}


}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,879
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Pairs in random numbers

 
0
  #8
Jun 30th, 2008
First: use code tags

next: void main

And then:
These lines:
  1. for(i=0;i<N;i++)
  2. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC