By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
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
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define LOW 1000000
  6. #define HIGH 2000000
  7.  
  8.  
  9.    int main()
  10.  {
  11.    int num_records;
  12.    int index;
  13.    int gpa1;
  14.    double gpa2;
  15.  
  16.    const int MIN_GPA = 0 ;
  17.    const int MAX_GPA = 4 ;
  18.    cout<< "Enter the number of records: \n";
  19.    cin>>num_records; 
  20.  
  21.    srand( time( NULL ));
  22.  
  23.    for( index=0 ; index< num_records; index++) {
  24.  
  25.    long id_number = rand() % (HIGH - LOW + 1) + LOW;
  26.  
  27.    cout<< id_number << endl;  
  28. }
  29.  
  30.       for(index=0; index < num_records ; index++) {
  31.  
  32.      int gpa1 = rand()  %   (MAX_GPA - MIN_GPA + 1) + MIN_GPA ; 
  33.  
  34.       }
  35.  
  36.       for( index=0 ; index < num_records ; index++) {
  37.  
  38.         gpa2 = (double)(rand()%100)/100;  
  39.  
  40.         }
  41.  
  42.         for(index=0; index < num_records ; index++) {
  43.  
  44.         cout<< gpa1 + gpa2 << endl; }
  45.  
  46. system("PAUSE");
  47.      }
  48.  
  49.  
Dec 17 '06 #1
Share this Question
Share on Google+
4 Replies


momotaro
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

Ganon11
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

Post your reply

Sign in to post your reply or Sign up for a free account.