# "Multiple Filter Randoms"

• 10-28-2007
gnimblegnome
"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.
• 10-28-2007
laserlight
Quote:

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.
• 10-28-2007
Wraithan
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         }     }```
• 10-28-2007
iMalc
Quote:

Originally Posted by gnimblegnome
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.