"Multiple Filter Randoms"

This is a discussion on "Multiple Filter Randoms" within the C++ Programming forums, part of the General Programming Boards category; This is something I am very proud of, no matter how simple it is. Yes there might be easier ways ...

  1. #1
    Registered User gnimblegnome's Avatar
    Join Date
    Oct 2007
    Location
    FL, USA
    Posts
    2

    "Multiple Filter Randoms"

    This is something I am very proud of, no matter how simple it is. Yes there might be easier ways to do this, but I like my way!

    Anywho, I set off making a more random rand() function. And I came up with this:

    Code:
    /********************************************************
    * Power Rand beta version 4
    * coded by Seth Collins
    * This function is a test of "Multiple Filter Randoms"
    * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    * useing Filtered Randoms is takeing a random (usu seeded
    * to the time.) and using it to assign a number to another
    * variable, witch is used to either add to the time function
    * of another filter, or the "real seed".
    * The example below is a three-fold filtered random
    *********************************************************/
    #include<iostream> 
    #include<ctime>
    using namespace std;
    
    int main(void)
    {
        //Psudo-srands, these following are used in the filters.
        int WRand; //"White Rand the first to be used
        int BRand; // Black Rand the third to be used
        int GRand; // Grey Rand the Second to be used
        int MRand; // Mega Rand the Fourth to be used
        int QRand; // Quintte Rand the 5th to be used
        //This is the "real seed"
        int xRand;
        // The Number to be randomed
        int iNum;
        
        while(true)
        {
        iNum = 0; // the number is reset        
        if(true){//forced if statment for organization
             srand(static_cast<unsigned int>(time(0))); // the root srand no modification
             WRand = rand()%10; //white random is assigned
             switch(WRand){//white rand switch
                            case 0: GRand = 10; break; // Gray Random is assigned
                            case 1: GRand = 9; break;
                            case 2: GRand = 1; break;
                            case 3: GRand = 8; break;
                            case 4: GRand = 2; break;
                            case 5: GRand = 7; break;
                            case 6: GRand = 3; break;
                            case 7: GRand = 6; break;
                            case 8: GRand = 4; break;
                            case 9: GRand = 5; break; 
                            default: cout << "WTF, mate? \n"; return -1; //error save
             }
        }
        if(true){
             srand(static_cast<unsigned int>(time(0)+GRand)); // Gray Seeded
             BRand = rand()%10; // Black is assigned
             switch(BRand){// Black switch
                            case 0: QRand = 2; break;// Quintte is assigned
                            case 1: QRand = 5; break;
                            case 2: QRand = 8; break;
                            case 3: QRand = 6; break;
                            case 4: QRand = 7; break;
                            case 5: QRand = 3; break;
                            case 6: QRand = 1; break;
                            case 7: QRand = 0; break;
                            case 8: QRand = 4; break;
                            case 9: QRand = 9; break; 
                            default: cout << "WTF, mate? \n"; return -1;
             }
        }         if(true){
             srand(static_cast<unsigned int>(time(0)+QRand)); // Quintte Seeded
             MRand = rand()%10;// Mega assingned
             switch(MRand){//Mega Switch
                            case 0: xRand = 100; break; // real srand assigned
                            case 1: xRand = 10; break;
                            case 2: xRand = 90; break;
                            case 3: xRand = 80; break;
                            case 4: xRand = 20; break;
                            case 5: xRand = 70; break;
                            case 6: xRand = 30; break;
                            case 7: xRand = 60; break;
                            case 8: xRand = 40; break;
                            case 9: xRand = 50; break; 
                            default: cout << "WTF, mate? \n"; return -1;
             }
             }
        srand(static_cast<int>(time(0)+xRand));// Real Seed
        iNum = rand()%100+1;// iNum assigned
        cout << iNum << "\n\n";//output
        system("PAUSE");//pause for clarity
        }
        return 0;// exit
        
    }
    I think it could use some fine tuning, but it is a pretty good start.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Anywho, I set off making a more random rand() function.
    hmm... it seems to me that you are not so much writing a "more random rand() function", but a more complicated way to obtain a seed for a random number generator. I suspect that it is not likely to be "more random" than just using time(0) right away. If someone knows the return value of the first time(0), he/she should be able to predict the final pseudorandom number sequence since the subsequent time(0) calls are unlikely to differ in return value from the first. Consequently, how "random" is the sequence still depends on the original time(0), and invariant factors like how good is the PRNG algorithm.

    Incidentally, you might want to read Prelude's article on Using rand() and her Random Numbers Tutorial.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Just a comment, in C and C++, you needn't have the useless if(true), you can just use brackets where ever you want as long they all match up.

    Code:
        if(true){//forced if statment for organization
             srand(static_cast<unsigned int>(time(0))); // the root srand no modification
             WRand = rand()&#37;10; //white random is assigned
             switch(WRand){//white rand switch
                            case 0: GRand = 10; break; // Gray Random is assigned
                            case 1: GRand = 9; break;
                            case 2: GRand = 1; break;
                            case 3: GRand = 8; break;
                            case 4: GRand = 2; break;
                            case 5: GRand = 7; break;
                            case 6: GRand = 3; break;
                            case 7: GRand = 6; break;
                            case 8: GRand = 4; break;
                            case 9: GRand = 5; break; 
                            default: cout << "WTF, mate? \n"; return -1; //error save
             }
        }
    is the same as
    Code:
        {// scoped for organization
             srand(static_cast<unsigned int>(time(0))); // the root srand no modification
             WRand = rand()%10; //white random is assigned
             switch(WRand){//white rand switch
                            case 0: GRand = 10; break; // Gray Random is assigned
                            case 1: GRand = 9; break;
                            case 2: GRand = 1; break;
                            case 3: GRand = 8; break;
                            case 4: GRand = 2; break;
                            case 5: GRand = 7; break;
                            case 6: GRand = 3; break;
                            case 7: GRand = 6; break;
                            case 8: GRand = 4; break;
                            case 9: GRand = 5; break; 
                            default: cout << "WTF, mate? \n"; return -1; //error save
             }
        }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by gnimblegnome View Post
    This is something I am very proud of, no matter how simple it is. Yes there might be easier ways to do this, but I like my way!

    Anywho, I set off making a more random rand() function. And I came up with this:

    I think it could use some fine tuning, but it is a pretty good start.
    Actually it's unfotunately not a good start at all.
    1. It is VERY long. There are much better entire PRNGs whose code is shorter than this.
    2. You're unnecessarily and incorrectly calling srand more than once in your program.
    3. Even if you remove the extra srand calls, you're calling rand more than necessary which shortens the preiod of repetition of the random sequence.
    4. It's slow because there are 4 division operations in there - the modulus (&#37. These make the result non-uniformly distributed as well, but that's another matter.

    Things to know:
    Calling srand more does not give more randomness, no matter how the seed as obtained. (in fact usually less randomness occurs as a result)
    Calling rand more does not give more randomness, no matter how you combine the results. Any perceived improvements are offset by the reduction in time to sequence repetition.
    Last edited by iMalc; 10-28-2007 at 01:30 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gabor Filter in C#
    By nanang in forum C# Programming
    Replies: 0
    Last Post: 05-17-2009, 09:22 PM
  2. Trouble adding source filter for WMV
    By abachler in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2008, 09:38 AM
  3. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  4. Simple Filter
    By 00Sven in forum C Programming
    Replies: 3
    Last Post: 03-14-2006, 08:46 PM
  5. Swear filter in a chat room.
    By adrianxw in forum Tech Board
    Replies: 9
    Last Post: 10-30-2003, 06:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21