473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

need help for randomizing numbers

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
4 1358
momotaro
357 100+
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
mrtcnr
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
3,652 Expert 2GB
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
mrtcnr
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

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

Similar topics

2
by: Susanna | last post by:
Hi all, I'm using the following slideshow script that I found on the web to display changing images with a crossfade effect. Eventually I will be adding many more images to the slideshow. The...
16
by: Mantorok Redgormor | last post by:
Not sure if this is possible, but is there some way to randomize a set of integers from 0 to n(more specifically 0 to 100 inclusive) without using rand() and in standard C?
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
6
by: Paul van Brenk | last post by:
When you run the Shuffle method often enough it will throw exception. And I can't figure out why. Anybody? Paul van Brenk the code: static void Shuffle(){ int ints = { 1, 2, 3, 4, 5, 6,...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
2
by: utahwrx | last post by:
I currently have a Javascript application that randomizes about 200 images. The problem is that the images preload, which causes the entire site to not come up until all the images are loaded. I'd...
2
by: andre.ditador9999 | last post by:
I want to randomize a certain calculation in Python but haven't figured it out yet. To explain what i mean, I' m going to use an example: I want to get the numbers to do a random experience...
1
by: EXotiX | last post by:
hey im new to vb6 and I am making a blackjack game but can not randomize the array correctly. Could you help please Option Explicit Const NumItems As Integer = 53 Public Sub RandomizeCards()...
2
by: blaziken | last post by:
can some1 teach me how to randomize numbers in vb similar in a vb slot machine. ur help is badly needed. 0_0
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.