Thread: Random Number Generator Class

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    Random Number Generator Class

    I wrote this for an encryption project Im working on. Thought you guys might like to use it if you have need of a cryptographic strength random number generator.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm pretty tired right now, but if you could explain how it works, it would help me a lot.

    From what I see, it looks like it creates a huge array of prime numbers < 65536, and a huge array of all 1's

    Then GetByte() does a calculation and saves it in the State array, and then assigns the result of another calculation to the pByte parameter.

    GetString() just calls GetByte() for each element of the string parameter.

    BTW, won't the Prime member array only have 6542 numbers in it and the rest would just be garbage left in memory? At least according to this site: http://primes.utm.edu/lists/small/10000.txt the first 6542 prime numbers go up to 65521.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    There's 2 functions to call.

    GetByte() obviously returns a random byte.
    GetString(BYTE* , DWORD) returns a string of length DWORD.
    Yes it just calls GetByte repeatedly.

    As for the theory behind how it generates the random numbers, maybe some other time

    BTW, check out www.anthonyqbachler.com for a much larger list of primes, and take another look at the code, it doesnt find the primes up to 65536, it finds 65536 primes.
    Last edited by abachler; 09-02-2008 at 11:18 PM.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It must take a long time to find all those prime numbers. Wouldn't it be better to store all those primes in a file and then read them in rather than re-calculating them all the time?

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    It takes under 400ms, once, when the class is created. Using a file would take a similar amount of time, require pregeneration by another program, and wouldn't be adjustable as easily.
    Last edited by abachler; 09-03-2008 at 08:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM