Right here is my working code generating a random number and then converting it into binary. Im having trouble trying to generate 3 random numbers. Any help would be appreciated.
Code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <cstring.h>
#include <ctype.h>
#include <math.h>
#define Population 10
#define Chrom_Length 4
void main(){
int Numbers [Population];
int Binary [Population][Chrom_Length];
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++)
{
Random = (rand()%MAX); // Generate the random number
Numbers[i] = Random; // Store the number
}
for(int i=0; i<Population; i++) // Population for the loop
{
cout<< Numbers[i]<<endl;
}
for(int i=0; i<Population; i++)
{
Decimal = Numbers[i];
for (int j=0; j<Chrom_Length; j++)
{ CurrSigBit = pow(2,(Chrom_Length-j-1));
if (CurrSigBit <= Decimal)
{
Binary[i][j]=1;
Decimal = Decimal - CurrSigBit;
}
else
{
Binary[i][j]=0;
}
}
}
for (int i=0; i<Population; i++)
{
for (int j=0; j<Chrom_Length; j++)
{
cout<<Binary[i][j];
}
cout<<endl;
}
}