Hi there.

I have scrolling for two days to find the code on how to make sure there is no repetition of numbers when randomly generate a set of arrays say a[i] and b[i], and let the number generated is between -20 and 20, for example. I am getting stuck here. I am a c++ beginner and really need help here.

Hope to hear some suggestion. thanks.

# no repeatition of numbers in two arrays

Page 1 of 1## 9 Replies - 750 Views - Last Post: 25 January 2018 - 06:39 AM

##
**Replies To:** no repeatition of numbers in two arrays

### #2

## Re: no repeatition of numbers in two arrays

Posted 23 January 2018 - 09:38 PM

What have you tried, thought about, or considered?

### #3

## Re: no repeatition of numbers in two arrays

Posted 23 January 2018 - 11:22 PM

Well why not try to solve the logic of the problem yourself instead of trying to find ready made code online.

you have two arrays say array_one[10] and array_two[10] be descriptive with your naming conventions calling them a or b is just lazy or better still use two vectors.

you generate a random number and add it to array_one and increment a

you then have a loop to generate the 9 other random numbers

loop counter to 10

generate random number

check to see if the number exists in array_one with a loop from 0 to

if it doesnt exist add the number to array_one at position

this will generate an array of random numbers that are unique.

next up is the second array do something similar but this time instead of just checking to see if the numbers are unique in array_two also check if the number exists in array_one which is fully populated at this point so a loop from 0 to the total size of array_one is needed and use an

For advanced users the problem could be simplified by the use of

http://www.cplusplus...gorithm/unique/

you have two arrays say array_one[10] and array_two[10] be descriptive with your naming conventions calling them a or b is just lazy or better still use two vectors.

you generate a random number and add it to array_one and increment a

**counter**variable by one.you then have a loop to generate the 9 other random numbers

loop counter to 10

generate random number

check to see if the number exists in array_one with a loop from 0 to

**counter**if it doesnt exist add the number to array_one at position

**counter**and increment**counter**else decrement outer loop variable by one.this will generate an array of random numbers that are unique.

next up is the second array do something similar but this time instead of just checking to see if the numbers are unique in array_two also check if the number exists in array_one which is fully populated at this point so a loop from 0 to the total size of array_one is needed and use an

**AND**in your if statement to do the cross checking.For advanced users the problem could be simplified by the use of

**std::unique**http://www.cplusplus...gorithm/unique/

This post has been edited by **snoopy11**: 23 January 2018 - 11:33 PM

### #4

## Re: no repeatition of numbers in two arrays

Posted 24 January 2018 - 03:12 AM

Another option is to use a std::set<int> and fill it with random numbers so they automatically will be unique.

Then you copy your set into the arrays or even better vectors.

Then you copy your set into the arrays or even better vectors.

### #5

## Re: no repeatition of numbers in two arrays

Posted 25 January 2018 - 12:56 AM

snoopy11, on 23 January 2018 - 11:22 PM, said:

Well why not try to solve the logic of the problem yourself instead of trying to find ready made code online.

you have two arrays say array_one[10] and array_two[10] be descriptive with your naming conventions calling them a or b is just lazy or better still use two vectors.

you generate a random number and add it to array_one and increment a

you then have a loop to generate the 9 other random numbers

loop counter to 10

generate random number

check to see if the number exists in array_one with a loop from 0 to

if it doesnt exist add the number to array_one at position

this will generate an array of random numbers that are unique.

next up is the second array do something similar but this time instead of just checking to see if the numbers are unique in array_two also check if the number exists in array_one which is fully populated at this point so a loop from 0 to the total size of array_one is needed and use an

For advanced users the problem could be simplified by the use of

http://www.cplusplus...gorithm/unique/

you have two arrays say array_one[10] and array_two[10] be descriptive with your naming conventions calling them a or b is just lazy or better still use two vectors.

you generate a random number and add it to array_one and increment a

**counter**variable by one.you then have a loop to generate the 9 other random numbers

loop counter to 10

generate random number

check to see if the number exists in array_one with a loop from 0 to

**counter**if it doesnt exist add the number to array_one at position

**counter**and increment**counter**else decrement outer loop variable by one.this will generate an array of random numbers that are unique.

next up is the second array do something similar but this time instead of just checking to see if the numbers are unique in array_two also check if the number exists in array_one which is fully populated at this point so a loop from 0 to the total size of array_one is needed and use an

**AND**in your if statement to do the cross checking.For advanced users the problem could be simplified by the use of

**std::unique**http://www.cplusplus...gorithm/unique/

### #6

## Re: no repeatition of numbers in two arrays

Posted 25 January 2018 - 01:12 AM

Ok do you have a point ?

Apart from just quoting me ?

Apart from just quoting me ?

### #7

## Re: no repeatition of numbers in two arrays

Posted 25 January 2018 - 01:36 AM

#include <iostream> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <math.h> #include <time.h> #define MAX 30 #define EPS 0.1 void repeated_root(void); void algorithm(double[]); int factorial (int); void Binomial (int,int); int i,j,k,n,t,order,iter,num_poly=0,round=0,orderp,orderq; double a[MAX],A[MAX],b[MAX],c[MAX],C[MAX],d[MAX],aa[MAX],binomial[MAX][MAX]; double root[MAX],alfa[MAX],z[MAX],rootc[MAX],rootp[MAX],rootq[MAX],roota[MAX],rootb[MAX]; double tmp1,tmp2; void main() { char Mychoice; puts("\n****COMPUTING COEFFICIENTS OF POLYNOMIAL WITH DIFFERENT BASES****"); while(1) { puts("\nPlease select 1:"); puts(" 1 repeated roots"); puts(" x Exit"); printf("My choice is "); Mychoice=getche(); if( Mychoice=='x' || Mychoice=='X') { printf("\n*******Goodbye*******\n"); return; } switch(Mychoice) { case '1':repeated_root(); return; } } } //=====repeated root void repeated_root() { int seq[MAX]; //define //Generate random roots of c between -20 to 20 srand((unsigned)time(NULL)); while ((order<1) || (order>10)) { printf("\nPolynomial c degree (2~10): "); scanf("%d",&order); } //Initialize seq to an ordered range for (i=-10;i<=10;i++) seq[i]=i; //Random shuffle for (i=-10;i<9;i++) { int r=(rand()%(10-i)+1); int save=seq[i]; seq[i]=seq[i+r]; seq[i+r]=save; } for (i=0;i<=10;i++) rootc[i]=seq[i]; //Generate random roots of p between -20 to 20/ srand((unsigned)time(NULL)); while ((orderp<1) || (orderp>10)) { printf("\nPolynomial p degree (1~10): "); scanf("%d",&orderp); } //Initialize seq to an ordered range for (i=-10;i<=10;i++) seq[i]=i; //Random shuffle for (i=-10;i<9;i++) { int r=(rand()%(10-i)+1); int save=seq[i]; seq[i]=seq[i+r]; seq[i+r]=save; } for (i=0;i<=10;i++) rootp[i]=seq[i]; //Generate random roots between -20 to 20 srand((unsigned)time(NULL)); while ((orderq<1) || (orderq>10)) { printf("\nPolynomial q degree (1~10): "); scanf("%d",&orderq); } //Initialize seq to an ordered range for (i=-10;i<=10;i++) seq[i]=i; //Random shuffle for (i=-10;i<9;i++) { int r=(rand()%(10-i)+1); int save=seq[i]; seq[i]=seq[i+r]; seq[i+r]=save; } for (i=0;i<=10;i++) rootq[i]=seq[i]; algorithm(root); } void algorithm(double root[]) { double tmp1,tmp2; //======Determine coefficients printf("\nInput roots are 1,2,...,n\n"); printf(" real imaginary\n"); printf("\nc is:\n"); for(i=1;i<=order;i++) { printf("rootc[%d]= %lf 0\n",i,rootc[i]); } printf("\np is:\n"); for(i=1;i<=orderp;i++) { printf("rootp[%d]= %lf 0\n",i,rootp[i]); } printf("\nq is:\n"); for(i=1;i<=orderq;i++) { printf("rootq[%d]= %lf 0\n",i,rootq[i]); } //caling for a printf("\na is:"); for(i=1;i<=order;i++) { roota[i]=rootc[i]; printf("\na[%d]= %lf ",i,roota[i]); } for(i=1+order;i<=order+orderp;i++) { roota[i]=rootp[i-order]; printf("\na[%d]= %lf ",i,roota[i]); } //caling for b printf("\n\nb is:"); for(i=1;i<=order;i++) { rootb[i]=rootc[i]; printf("\nb[%d]= %lf ",i,rootb[i]); } for(i=1+order;i<=order+orderq;i++) { rootb[i]=rootq[i-order]; printf("\nb[%d]= %lf ",i,rootb[i]); } getch(); }

this is a part of the coding that i get from a senior which i dont fully understand as i am new to this c++.

but basically what i want is to generate rooota[i] and rootb[i] such that roota[i](combination of rootc[i] and rootp[i]) and rootb[i](rootc[i] and rootq[i]). now i am stuck to make sure that i dont generate the same random number in either rootc[i], rootp[i] and rootq[i] (because i dont really know where to edit and what to edit actually)

I am not very familiar with std::unique because all i've done up to now is basic c++ programming without using any class/function/subfunction.

I thanks @snoopy11 for telling me your thoughts because that is actually what i meant. but still after googling, i dont quite understand.

snoopy11, on 25 January 2018 - 01:12 AM, said:

Ok do you have a point ?

Apart from just quoting me ?

Apart from just quoting me ?

that was a mistake because i am still learning how to use this site.

### #8

## Re: no repeatition of numbers in two arrays

Posted 25 January 2018 - 01:49 AM

Yeah...

I totally broke it down for you into manageable pieces...

It will take work and effort to code those individual pieces but my suggestion to you now is get to work...

I totally broke it down for you into manageable pieces...

It will take work and effort to code those individual pieces but my suggestion to you now is get to work...

### #9

## Re: no repeatition of numbers in two arrays

Posted 25 January 2018 - 05:00 AM

Are you sure you want to write C++, the code you posted is just old C apart from #include <iostream>

In arrays the first index is 0 and the last index is array size - 1 so this will most likely crash:

In arrays the first index is 0 and the last index is array size - 1 so this will most likely crash:

for (i=-10;i<=10;i++) seq[i]=i;

### #10

## Re: no repeatition of numbers in two arrays

Posted 25 January 2018 - 06:39 AM

abai, on 25 January 2018 - 03:36 AM, said:

but basically what i want is to generate rooota[i] and rootb[i] such that roota[i](combination of rootc[i] and rootp[i]) and rootb[i](rootc[i] and rootq[i]). now i am stuck to make sure that i dont generate the same random number in either rootc[i], rootp[i] and rootq[i] (because i dont really know where to edit and what to edit actually)

This is pretty easy to do. In pseudo code:

Let array source, but an array that can hold N * 3 numbers where N is the size of arrays rootc, rootp, and rootq Fill source with unique random numbers for(int i = 0; i < N; i++) { rootc[i] = source[i]; rootp[i] = source[N + i]; rootq[i] = source[N * 2 + i]; }

So now, you are just back to your original problem of generating an array of unique "random" numbers.

Personally, I take issue with calling this "random". Consider the case of flipping a coin. If you first flip heads, that means that next "unique" random coin flip MUST be tails, otherwise it is not unique. How random is that if you can predict what the next number will be? Random is supposed to be random. You have no guarantees whether the next random number you get will be the same as a number that you had previously seen.

As noted in the code that was given to you by your friend, the correct word is "shuffle".

Page 1 of 1