943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1619
  • C++ RSS
Jun 28th, 2008
0

Pairs in random numbers

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abacian1983 is offline Offline
4 posts
since Jun 2008
Jun 28th, 2008
0

Re: Pairs in random numbers

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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Jun 28th, 2008
0

Re: Pairs in random numbers

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";}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abacian1983 is offline Offline
4 posts
since Jun 2008
Jun 28th, 2008
0

Re: Pairs in random numbers

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 29th, 2008
0

Re: Pairs in random numbers

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abacian1983 is offline Offline
4 posts
since Jun 2008
Jun 29th, 2008
0

Re: Pairs in random numbers

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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Jun 30th, 2008
0

Re: Pairs in random numbers

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++;}


}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abacian1983 is offline Offline
4 posts
since Jun 2008
Jun 30th, 2008
0

Re: Pairs in random numbers

First: use code tags

next: void main

And then:
These lines:
cpp Syntax (Toggle Plain Text)
  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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: warning C4715: 'Wagering::wageringTest' : not all control paths return a value
Next Thread in C++ Forum Timeline: Search for a word in a dictionary





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC