PDA

Click to See Complete Forum and Search --> : True Random Number Generation


Kasracer
Aug 8th, 2003, 01:06 AM
Okay, I am still new at C++, but I though I'd share this. Maybe it will be useful to others :)

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 :)

bugzpodder
Aug 18th, 2003, 09:54 AM
<boost/lexical_cast.hpp>
what does that do? i dont think its included in the standard library or STL

Kasracer
Aug 18th, 2003, 12:35 PM
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
Aug 18th, 2003, 12:41 PM
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
Aug 18th, 2003, 05:12 PM
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
Aug 23rd, 2003, 01:25 PM
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
Aug 23rd, 2003, 03:26 PM
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
Sep 5th, 2003, 03:05 AM
kasracer.... ALL WRONG!


:D :D

Kasracer
Sep 5th, 2003, 01:08 PM
Originally posted by dis1411
kasracer.... ALL WRONG!


:D :D but......... it works... :(

parksie
Sep 12th, 2003, 12:04 PM
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
Sep 16th, 2003, 01:31 PM
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
Sep 16th, 2003, 01:36 PM
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.