Thread: Help generating multiple random numbers

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

    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;
    		}
     }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

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

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

    add 'using namespace std;' after your includes
    Last edited by nadroj; 10-30-2006 at 12:38 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i know exactly what ones are needed and why. im trying to make people think for themself with as few 'answers' i can give.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i know exactly what ones are needed and why. im trying to make people think for themself with as few 'answers' i can give.
    That explanation wasn't for you, it was for the OP. I understand completely the desire to make people think for themselves. In this case, I felt the suggestion to learn which headers are required and when is not always the easiest task, so giving a more detailed explanation so the OP can learn that is appropriate.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok, cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. generating 10 unique random numbers in C
    By creeping death in forum C Programming
    Replies: 4
    Last Post: 01-28-2009, 11:23 AM
  3. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 01:51 AM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. problems with generating random numbers
    By pcwizzzz in forum C Programming
    Replies: 10
    Last Post: 02-16-2003, 05:16 PM