Thread: Help with number generation

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    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;
     }
     }
    Last edited by d-dub; 11-20-2006 at 08:04 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    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

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    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;
    
    }
    
     }

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  2. random number generation
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-26-2007, 04:51 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. fast random number generation
    By Heraclitus in forum C Programming
    Replies: 4
    Last Post: 02-09-2003, 07:48 PM