1. ## 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. 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. 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. Originally Posted by Robster66
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.

Originally Posted by robster66
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. 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. 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. 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. Originally Posted by Robster66
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.

9. 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. None of the plethora of benchmarking programs on the internet is good enough for you?

11. Hahaha, I just wanted something small, simple, and of course made by me.

12. 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.