454,137 Members | 1,024 Online Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

# need help for randomizing numbers

 P: 6 Hi everyone! I have a problem with my code. At that program I am randomly creating ID NUMBER and then GPA. First I enter the number of records.And then create ID NUMBERS for students. Since then everything is OK. But I am having some problems when creating randomly GPA's between 0.00-4.00 . First i tried to create randomly GPA's but then i had some errors. So I tried something different. I randomly create gpa1 between 0-4 and then randomly create gpa2 between 0-1 and then add gpa1 to gpa2 , which gives me a double number between 0-4. But when I run the program and let's say there are 3 students , it outputs 3 different ID NUMBER s randomly but it does not output 3 different GPA. It just randomizes a GPA and then outputs the same for all students. I hope I managed to explain my problem. How can I solve this? Thanks in advance. Here is the code I have: Expand|Select|Wrap|Line Numbers #include  #include  #include    #define LOW 1000000 #define HIGH 2000000        int main()  {    int num_records;    int index;    int gpa1;    double gpa2;      const int MIN_GPA = 0 ;    const int MAX_GPA = 4 ;    cout<< "Enter the number of records: \n";    cin>>num_records;       srand( time( NULL ));      for( index=0 ; index< num_records; index++) {      long id_number = rand() % (HIGH - LOW + 1) + LOW;      cout<< id_number << endl;   }         for(index=0; index < num_records ; index++) {        int gpa1 = rand()  %   (MAX_GPA - MIN_GPA + 1) + MIN_GPA ;          }         for( index=0 ; index < num_records ; index++) {           gpa2 = (double)(rand()%100)/100;             }           for(index=0; index < num_records ; index++) {           cout<< gpa1 + gpa2 << endl; }   system("PAUSE");      }     Dec 17 '06 #1
4 Replies

 100+ P: 357 do some research on how random function work! :) HINT!!!: "seed" enjoy! don't give him a fish...teach him how to fish!!! Dec 18 '06 #2

 P: 6 do some research on how random function work! :) HINT!!!: "seed" enjoy! don't give him a fish...teach him how to fish!!! I think my randomizing is ok but I have a problem with my for loop and I still cannot solve it. Dec 18 '06 #3

 Expert 2.5K+ P: 3,652 I think the problem is that you define gpa1 and gpa2 inside your loops - this means that, outside those loops, gpa1 and gpa2 do not exist. Also, you calculate a random GPA several times, but you neer do anything to it - should you be setting some variable each time you calculate the values? Dec 18 '06 #4

 P: 6 I think the problem is that you define gpa1 and gpa2 inside your loops - this means that, outside those loops, gpa1 and gpa2 do not exist. Also, you calculate a random GPA several times, but you neer do anything to it - should you be setting some variable each time you calculate the values? I think I defined gpa1 and gpa2 outside the loop. As you see I defined them just after int main(). The reason I calculate GPA several times is because I couldnt randomly generate decimal number. That's why I tried to generate randomly between 0-4 which i called as gp1 and then between 0-1 which i called as gpa2.. Finally I add gpa1 to gpa2 to get a decimal number like 2.54. Dec 18 '06 #5 