Results 1 to 12 of 12

Thread: True Random Number Generation

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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

  2. #2
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    <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)!

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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.

  4. #4
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    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)!

  5. #5

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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.

  6. #6
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    is this really true random? i dont think so...you're still using rand() to get the numbers and they're not true random.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  7. #7

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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.

  8. #8
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    kasracer.... ALL WRONG!



  9. #9

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by dis1411
    kasracer.... ALL WRONG!


    but......... it works...

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width