# Help with number generation

• 11-20-2006
d-dub
Help with number generation
Here is my code. I need to generate three random numbers and produce the binary equivilent for each. I have got so far with it but now it produces a load of garbage and im struggling to see why.

Code:

``` #include <iostream.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <math.h> #define Population 10 #define Chrom_Length 4 #define Dimension 3  void main(){         int Numbers [Population][Dimension];         int Binary [Population][Chrom_Length*Dimension];           int Position;           int Random;           int Decimal;           int MAX = pow(2,Chrom_Length)-1;           int MostSigBit = pow(2,(Chrom_Length-1));           int CurrSigBit;   srand ( time(NULL));    // Seeds the random number generator   for(int i=0; i<Population; i++)   {   for(int j=0; j<Dimension;j++)   {         Random = (rand()%MAX);        // Generate the random number         Numbers[i][j] = Random;        // Store the number                 }       }         for(int i=0; i<Population; i++)  // Population for the loop   {     for(int i=0; i<Population; i++)   {   Position =0;       for (int d=0; d< Dimension; d++)  // Populates the dimension   Decimal = Numbers[i][d];         for (int j=0; j<Chrom_Length; j++)  // Looks at each part of the chromosome in the loop         {         CurrSigBit = pow(2,(Chrom_Length-j-1));                                 if (CurrSigBit <= Decimal)                           {                                         Binary[i][j]=1;               Decimal = Decimal - CurrSigBit; // Takes the CurrSigBit value away from the current decimal value                       }                               else               {               Binary[i][j]=0;  // If CurrSigBit is greater than the current decimal number store a zero in the binary array               }               Position++;   }   }         }   for (int i=0; i<Population; i++)                 {               for (int d=0; d< Dimension; d++)                       {             cout<< Numbers[i][d];             cout<< " " ;         for(int j=Position; j< (Chrom_Length+Position); j++)                                         {               cout<<Binary[i][j];               }               Position = Position + Chrom_Length;               }             cout<<endl;  }  }```
• 11-20-2006
vart
Do you know what value has Position when you enter the loop?
Code:

```for (int i=0; i<Population; i++)                 {               for (int d=0; d< Dimension; d++)                       {             cout<< Numbers[i][d];             cout<< " " ;         for(int j=Position; j< (Chrom_Length+Position); j++)                                         {               cout<<Binary[i][j];               }               Position = Position + Chrom_Length;               }             cout<<endl;  }```
do you know what maximum value for j you achieve?
could you use more clear indentation? (I couldn't read the code till I copied it to VC and pressed Alt+F8)
• 11-20-2006
d-dub
Position is set to move from each number via the chrom_length. So the first random number is 0-3 in chrom_length, number 2 is 4-7 etc. So the position knows to generate the 2nd random number when it reaches the 4th postion in chrom_lenght
• 11-20-2006
manutd
1)
Code:

`void main()`
Should be
Code:

`int main()`
2)
Code:

```#define Population 10 #define Chrom_Length 4 #define Dimension 3 ... int MAX = pow(2,Chrom_Length)-1;```
Capitals are usually used to denote defined constants, whereas lowercase is used for variables.
3) Try using proper indentation so that your for loops are readable.
• 11-20-2006
vart
Quote:

Originally Posted by d-dub
Position is set to move from each number via the chrom_length. So the first random number is 0-3 in chrom_length, number 2 is 4-7 etc. So the position knows to generate the 2nd random number when it reaches the 4th postion in chrom_lenght

I think - you should debug your program and see the actual values of this variable...

or add some traces and see what values are achived at what part of the code...
• 11-23-2006
d-dub
Update. Have it generating the three numbers now but the binary conversion doesn't work

Code:

```#include <iostream.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <math.h> #define Population 10 #define Chrom_Length 4 #define Dimension 3  void main(){         int Numbers [Population][Dimension];         int Binary [Population][Chrom_Length*Dimension];   int Position;   int Random;   int Decimal;   int MAX = pow(2,Chrom_Length)-1;   int MostSigBit = pow(2,(Chrom_Length-1));   int CurrSigBit;   srand ( time(NULL));    // Seeds the random number generator           for(int i=0; i<Population; i++)                         {           for(int j=0; j<Dimension;j++)                   {         Random = (rand()%MAX);        // Generate the random number         Numbers[i][j] = Random;   cout<<Numbers [i][j];   cout<<" ";        // Store the number         }   cout<<endl;   }         for(int i=0; i<Population; i++)  // Population for the loop           {             for(int i=0; i<Population; i++)                   {     int Position =0;       for (int d=0; d< Dimension; d++)  // Populates the dimension   Decimal = Numbers[i][d];         for (int j=0; j<Chrom_Length; j++)  // Looks at each part of the chromosone in the loop         {         CurrSigBit = pow(2,(Chrom_Length-j-1));                                 if (CurrSigBit <= Decimal)                           {                                         Binary[i][Position]=1;               Decimal = Decimal - CurrSigBit; // Takes the CurrSigBit value away from the current decimal value                       }                               else               {               Binary[i][Position]=0;  // If CurrSigBit is greater than the current decimal number store a zero in the binary array               }               Position++;               }   }         }   for (int i=0; i<Population; i++)                 {             int position=0;               for (int d=0; d< Dimension; d++)                       {         for(int j=position; j< (Chrom_Length+position); j++)                                         {               cout << Binary[i][j];       }         cout << " ";         position = position + Chrom_Length;   }       cout << endl; }  }```
• 11-23-2006
manutd
Take off the .h's:
Code:

```#include <iostream> #include <cstdlib> #include <cstdio> #include <time.h> #include <cmath>```
EDIT: Did you follow any of my instructions?