# VBForums CodeBank > CodeBank - C++ >  True Random Number Generation

## Kasracer

Okay, I am still new at C++, but I though I'd share this. Maybe it will be useful to others  :Smilie: 

I wrote 3 functions to generate and return true random numbers. I would like to add that, the numbers you pass to these functions *DO NOT* require you to add an extra number like you normally would do with random number equations.

Required Includes:


```
#include <iostream>
#include <windows.h>
#include <vector>
#include <boost/lexical_cast.hpp>
```

This first function will take in the highest number you want it to allow to generate, then return the random number as an integer


```
int RandomIntAsInt(int UpperBound) 
{
    UpperBound++;
	srand(GetTickCount());
	int random;
	random = rand()%UpperBound;
	return random;

}
```

This next function will take in an integer and return a string of the random number, to save you the work of converting it.


```
string RandomIntAsString(int UpperBound) 
{
	UpperBound++;
	srand(GetTickCount());
	int random;
	random = rand()%UpperBound;
	string total = "";
	total = boost::lexical_cast<std::string>(random);
	return total;
}
```

This last function takes in the max you want the random number to get to, and how many numbers you want to generate. It then returns a string with each number spaced apart with 1 space.


```
string RandomNumbersString(int UpperBound, int TotalNumbers) 
{

	srand(GetTickCount());
	int loop = 0;
	vector<int> random(TotalNumbers);
	int N = 0;
	for(loop = 0; loop < TotalNumbers; loop++){
		random[N] = rand()%UpperBound;
		N++;
	}
	string temp = "";
	string total = "";
	int stringloop;
	temp = boost::lexical_cast<std::string>(random[0]);
	total = total + temp;
	N = 1;
	--TotalNumbers;
	for(stringloop = 0; stringloop < TotalNumbers; stringloop++){
		temp = boost::lexical_cast<std::string>(random[N]);
		total = total + " " + temp;
		N++;
	}
	return total;
```

*NOTE:* I am still a n00b at C++, so I don't have anything awesome to share. I just thought someone may benefit from this  :Smilie:

----------


## bugzpodder

<boost/lexical_cast.hpp>
what does that do?  i dont think its included in the standard library or STL

----------


## Kasracer

> _Originally posted by bugzpodder_ 
> *<boost/lexical_cast.hpp>
> what does that do?  i dont think its included in the standard library or STL*


 The standard library IS STL.

Saying that, it's the boost library. It came with my distro of MinGW and I copied it into my VS .NET includes as well.

The library I included is neccessary to convert the integers into strings.

----------


## bugzpodder

STL is standard template library, which differs from other standard headers such as sstream, iostream, which i believe is not part of of the standard template library.

does your random number generator generate negative integers?  reals?

----------


## Kasracer

> _Originally posted by bugzpodder_ 
> *STL is standard template library, which differs from other standard headers such as sstream, iostream, which i believe is not part of of the standard template library.
> 
> does your random number generator generate negative integers?  reals?*


 I've sent negative numbers to it and it worked, just didn't use the negative number sign with it..... it goes from 0 to whatever number to specify that an integer can hold.

----------


## cyborg

is this really true random? i dont think so...you're still using rand() to get the numbers and they're not true random.

----------


## Kasracer

> _Originally posted by cyborg_ 
> *is this really true random? i dont think so...you're still using rand() to get the numbers and they're not true random.*


 I am also using GetTickCount() so the numbers are random.

The random numbers are drawn randomly based on the lenght in which the system has been operating at, so they are random.

----------


## dis1411

kasracer.... ALL WRONG! 


 :Big Grin:   :Big Grin:

----------


## Kasracer

> _Originally posted by dis1411_ 
> *kasracer.... ALL WRONG! 
> 
> 
>  *


 but......... it works...  :Frown:

----------


## parksie

Better pseudorandom numbers are seeded with an entropy pool. Take a look at the Unix /dev/random. In OSes like OpenBSD, this supply of random numbers is seeded from things like network packet split time, mouse movements, user process operations, and suchlike.

----------


## CornedBee

You're using a time measurement and a pseudorandom number generator. Your numbers cannot possibly be really random. Actually your numbers are worse than those of rand() because you seed the generator every time GetRandomIntAsInt is called. That means that code like this:


```
cout << GetRandomIntAsInt() << ' ' << GetRandomIntAsInt() << ' ' << GetRandomIntAsInt() << ' ' << endl;
```

will likely write out three identical numbers.

As parksie said, really random numbers are generated from other sources. One I'd like to add is the sound card.

Finally, about STL and the standard library.
STL, the Standard Template Library, was the first part of the C++ libraries to be somewhat standardized. It included classes like vector, list, deque etc. which were in the appropriate (.h) headers.
The standardization of C++ brought two changes. First, the .h ending was removed. Modern headers don't have it. Second, the C++ Standard Library was defined, and it included as a subset the STL.
So STL simply refers to all those containers, no matter if they are the old or the new versions.

----------


## parksie

One note about the new C++ headers...they're not guaranteed to be files, so, for example:

```
#include <iostream>

int main() { std::cout << "Hi!" << endl; }
```

This will not necessarily just whack /usr/include/iostream into the top of your file, the compiler/preprocessor could put the code in from anywhere. All the inclusion guarantees is that the features supplied by "iostream" become available, *somehow*.

----------

