Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Random Numbers

    I am trying to learn and the program goes through a loop starting at 0 to 300000. Than I just want it to generate a random number. Ultimately what I want to do is do a GUI speed test for the computer that goes through a loop from 0-1million and see how long it takes to finish.

    Code:
    #include <iostream>  // Includes cout, endl, cin.get, etc.
    #include <cstdlib>  // Includes rand()
    
    using namespace std;
    
    int main()
    {
        int loop = 0;
        
        while ( loop <= 300000 ) {
              cout << loop << endl;
              loop++;
              }
              cin.get();
              }
           
           int sparky()    
    {
           int randomnumber = rand();
           
           cout << randomnumber << endl;
           cin.get();
           }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't define a function inside another function. If you want sparky to be a function, define it before main, somewhere, and then just call it from inside main.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    How would it be inside another function if I ended main before sparky? Just reading sparky is funny. Is rand defined already with a certain set of numbers or do I have to do that also?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Robster66 View Post
    How would it be inside another function if I ended main before sparky?
    It wouldn't. That's kind of the point. Having one function inside another is highly illegal. That's why you wouldn't want sparky inside another function, 'cause it's illegal to do such a thing.

    Quote Originally Posted by robster66 View Post
    Just reading sparky is funny. Is rand defined already with a certain set of numbers or do I have to do that also?
    Have you looked up rand at all?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    I have looked it up but I do not completely understand. What I am saying is that how is it inside of main if main is ended before sparky starts?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hey you do have two curly braces there. Never mind.

    So there you go. The thing was working when we started.

    So what's the question, then.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    I am trying to learn about random numbers and what I read is that it is timed so at the time it picks what number it is at. It's hard for me to explain. Say I do a random number from 1-100 it seems by what I read it will mostly be lower digits such as under 50.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Robster66 View Post
    I am trying to learn about random numbers and what I read is that it is timed so at the time it picks what number it is at.
    That's an ... interesting idea.

    You should read about how pseudo-random number generators work in C++.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    I am going to read that. I am just very anxious to make this program that I want. Kind of like a test for every computer.

    -GUI
    -Timer
    -0-1 Million

    Just to see how long it takes with every computer, so I am trying to learn quick! = D

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    None of the plethora of benchmarking programs on the internet is good enough for you?

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Hahaha, I just wanted something small, simple, and of course made by me.

  12. #12
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    As written, your program will never generate a random number. There is nothing wrong necessarily with the sparky() function, but you never call it. You might want to change it to void sparky() however if you don't intend to return a value from the function. Otherwise, to be complete, it should return an int value before the closing curly brace.

    Also, the way you have indented the closing curly braces, cin.get() and int sparky() makes it harder to follow the program than it should be, which is probably why tabstop missed it the first time (as did I when I first looked at it).

    This would make it much easier to follow:
    Code:
    int main()
    {
        int loop = 0;
        
        while ( loop <= 300000 ) {
              cout << loop << endl;
              loop++;
        }
        cin.get();
    }
           
    int sparky()    
    {
        int randomnumber = rand();
           
        cout << randomnumber << endl;
        cin.get();
    }
    The formatting suggestion may seem nitpicky, but getting into the habit of keeping your code consistent like this will help in the long run, especially if you ever work with other programmers.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM