|
-
Aug 8th, 2003, 12:06 AM
#1
True Random Number Generation
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:
Code:
#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
Code:
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.
Code:
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.
Code:
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
-
Aug 18th, 2003, 08:54 AM
#2
Fanatic Member
<boost/lexical_cast.hpp>
what does that do? i dont think its included in the standard library or STL
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Aug 18th, 2003, 11:35 AM
#3
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.
-
Aug 18th, 2003, 11:41 AM
#4
Fanatic Member
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?
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Aug 18th, 2003, 04:12 PM
#5
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.
-
Aug 23rd, 2003, 12:25 PM
#6
Frenzied Member
is this really true random? i dont think so...you're still using rand() to get the numbers and they're not true random.
-
Aug 23rd, 2003, 02:26 PM
#7
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.
-
Sep 5th, 2003, 02:05 AM
#8
-
Sep 5th, 2003, 12:08 PM
#9
-
Sep 12th, 2003, 11:04 AM
#10
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 16th, 2003, 12:31 PM
#11
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:
Code:
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 16th, 2003, 12:36 PM
#12
Monday Morning Lunatic
One note about the new C++ headers...they're not guaranteed to be files, so, for example:
Code:
#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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|