# Help generating multiple random numbers

• 10-30-2006
d-dub
Help generating multiple random numbers
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;                 }  }```
• 10-30-2006
Quote:

Im having trouble trying to generate 3 random numbers.
what are you having trouble with EXACTLY? its easier if you tell us what we're to be looking for.
• 10-30-2006
d-dub
Well where it generates 1 number and the binary equivilent, i now need to make it generate 3 numbers and convert to the binary equivilent. I dont know where to start so any guidence will be appreciated
• 10-30-2006
how did you get the first random number? how do you expect to get the next one?

edit: also, for C++ you should use int main not void main, and remove the '.'h from all the #includes, those are for C. however, for 'time.h' it would have to be 'ctime', for example, same goes with 'math.h'. additionally, you have included headers you dont use. use trial and error, commenting one out and trying to compile to see what ones you dont use.

• 10-30-2006
Daved
>> use trial and error, commenting one out and trying to compile to see what ones you dont use
I think it would be better to learn which headers are required and when, and then use the appropriate ones. Sometimes code will compile without the proper header on some platforms, but not on others. The only ones I can see that are needed are <iostream> (for cout), <cstdlib> (for srand and rand), <ctime> (for time) and <cmath> (for pow). Technically <ostream> is also needed for endl, too.
• 10-30-2006