Random strings of character and some stats

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Fran?ois Lacrampe

    #1

    Random strings of character and some stats

    Hello,

    I've got two random number/statistics questions I'd like you to
    review. My first question is not directly related to PHP, but will be
    implemented in PHP, as explained in my second question, so let's go:

    I want to generate 10000 strings of x characters, with one chance (or
    less) on a million that you can guess them by just randomly typing
    them. So I need to know what is the value of x.

    I wrote the following equation :

    36^x/10000 = 1000000
    <=> 36^x = 10000 * 1000000
    <=> 36^x = 1010
    <=> x = ln(1010)/ln(36)
    <=> x = 23.025850929940 456840179914546 844/3.5835189384561 100016249547167 614
    <=> x = 6.4254860446923 437997173954827 712

    So, a 7-characters string would be good enough.

    So my first question is: is my reasoning OK? Knowing my math
    abilities, I doubt it very much! ;)

    The second question I have is related to PHP's rand() function. I've
    read many times that rand() is not random enough, especially when
    generating long lists of this kind. Would you use something that's
    more powerful than rand(), are there stronger random functions, within
    PEAR for instance, or anything?

    Thanks,
    JFLac
  • Andy Hassall

    #2
    Re: Random strings of character and some stats

    On 2 May 2005 13:55:18 -0700, jflacrampe@gmai l.com (Jean-Fran?ois Lacrampe)
    wrote:
    [color=blue]
    >I've got two random number/statistics questions I'd like you to
    >review. My first question is not directly related to PHP, but will be
    >implemented in PHP, as explained in my second question, so let's go:
    >
    >I want to generate 10000 strings of x characters, with one chance (or
    >less) on a million that you can guess them by just randomly typing
    >them. So I need to know what is the value of x.[/color]

    I'm not 100% clear on the "them" in the sentence; are you saying you want less
    than 1/1000000 chance of guessing ONE of the 10000 strings, or 1/1000000 chance
    of guessing the ENTIRE SET of 10000 strings?
    [color=blue]
    >I wrote the following equation :
    >
    > 36^x/10000 = 1000000[/color]

    Depends on the interpretation above. Not sure I get how the 10000 is involved
    here though.
    [color=blue]
    ><=> 36^x = 10000 * 1000000
    ><=> 36^x = 1010[/color]

    10000 * 1000000 = 1010 ? Is that supposed to be 10^10 ?
    [color=blue]
    ><=> x = ln(1010)/ln(36)
    ><=> x = 23.025850929940 456840179914546 844/3.5835189384561 100016249547167 614[/color]

    Apparently so :-)
    [color=blue]
    ><=> x = 6.4254860446923 437997173954827 712
    >
    >So, a 7-characters string would be good enough.[/color]

    If you want at worst 1/1000000 chance of guessing any string, isn't the number
    of strings irrelevant if they're random?

    i.e. it's just

    36^x > 1000000
    => x > ln(1000000)/ln(36)
    => x > 3.855

    So minimum number of chars = 4.

    (36^3 = 46656, 36^4 = 1679616)


    The odds of guessing ALL the strings surely head well out of the 1 in 1000000
    range for 10000 strings very quickly...
    [color=blue]
    >So my first question is: is my reasoning OK? Knowing my math
    >abilities, I doubt it very much! ;)
    >
    >The second question I have is related to PHP's rand() function. I've
    >read many times that rand() is not random enough, especially when
    >generating long lists of this kind. Would you use something that's
    >more powerful than rand(), are there stronger random functions, within
    >PEAR for instance, or anything?[/color]

    mt_rand() uses the Mersenne Twister pseudorandom algorithm, which is typically
    better (and as a bonus it's faster too).

    If you want to get really serious you'll need to base it on some sort of truly
    physical phenomenon, e.g. with RNG hardware, which is often based on random
    thermal fluctuations.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Chung Leong

      #3
      Re: Random strings of character and some stats


      Andy Hassall wrote:[color=blue]
      > If you want at worst 1/1000000 chance of guessing any string, isn't[/color]
      the[color=blue]
      > number of strings irrelevant if they're random?[/color]

      No, because each additional string means an additional try. Think of it
      this way: A person comes with a number of letters randomly and the
      computer tries 10000 times to guess it.

      Comment

      • Jean-François Lacrampe

        #4
        Re: Random strings of character and some stats

        Andy Hassall wrote:[color=blue]
        > I'm not 100% clear on the "them" in the sentence; are you saying you[/color]
        want less[color=blue]
        > than 1/1000000 chance of guessing ONE of the 10000 strings, or[/color]
        1/1000000 chance[color=blue]
        > of guessing the ENTIRE SET of 10000 strings?[/color]

        I meant the odds of guessing any of the 10000 strings, of course! :-)
        The odds of guessing the entire set must be really, really low!
        [color=blue]
        > 10000 * 1000000 = 1010 ? Is that supposed to be 10^10 ?[/color]

        Well, I wrote the equation in another editor who was sooo happy to show
        me it was able to display the 10^10 graphically. Too bad it forgot to
        copy/paste it back to me with the circumflex.
        [color=blue]
        > If you want at worst 1/1000000 chance of guessing any string, isn't[/color]
        the number[color=blue]
        > of strings irrelevant if they're random?[/color]

        Well, keeping in mind my very 'intuitive' and weak knowledge of math,
        I'd guess that the more strings you put in the list, the more chances
        you have to guess one (any) of them. If for instance I had a list long
        enough to contain all the possible combinations, the odds would be 1/1,
        right? If you divide the list by two, the odds are 1/2. And so on.
        So the number of items in the list seems to matter: that's how I came
        with the 10000 * 1000000 thing (by doing lots of intermediate and
        stupid steps on a sheet of paper).

        I'm not sure at all that I put the 10000 where I should have in the
        equation, though, hence my initial question.

        Now, I'm talking about things I don't understand (math) in a language
        that isn't my native language and I reckon that I'm a bit awkward at
        explaining my thoughts. :-)
        [color=blue]
        > mt_rand() uses the Mersenne Twister pseudorandom algorithm, which is[/color]
        typically[color=blue]
        > better (and as a bonus it's faster too).
        >
        > If you want to get really serious you'll need to base it on some[/color]
        sort of truly[color=blue]
        > physical phenomenon, e.g. with RNG hardware, which is often based on[/color]
        random[color=blue]
        > thermal fluctuations.[/color]

        I could also use a webcam on a lava lamp and produce my results using
        the webcam info, but I guess I don't need that randomness. I just
        wanted to know what was my best bet with what PHP can give me, with
        minimal hassle. ;-)

        Thanks for your answers,
        JFLac

        Comment

        • NC

          #5
          Re: Random strings of character and some stats

          Jean-Francois Lacrampe wrote:[color=blue]
          >
          > I want to generate 10000 strings of x characters, with one chance (or
          > less) on a million that you can guess them by just randomly typing
          > them. So I need to know what is the value of x.[/color]

          OK, one in a million chance of successfully guessing 10,000 strings
          equals 0.9986 chance of successfully guessing a single string:

          0.9986 ^ 10000 = 8.23412E-07 ~ 1E-06 (one in a million)

          In other words, even if you are virtually certain to get a single
          string right, it's still virtually impossible to get 10,000 of them
          right. So a one-character string will suffice. In fact, even a
          one-bit value (0 or 1) would be an overkill. :)
          [color=blue]
          > The second question I have is related to PHP's rand() function. I've
          > read many times that rand() is not random enough, especially when
          > generating long lists of this kind. Would you use something that's
          > more powerful than rand(), are there stronger random functions,[/color]
          within[color=blue]
          > PEAR for instance, or anything?[/color]

          Check out mt_rand():

          Generate a random value via the Mersenne Twister Random Number Generator


          Cheers,
          NC

          Comment

          • Jean-François Lacrampe

            #6
            Re: Random strings of character and some stats

            NC wrote:[color=blue]
            > Jean-Francois Lacrampe wrote:[color=green]
            > >
            > > I want to generate 10000 strings of x characters, with one chance[/color][/color]
            (or[color=blue][color=green]
            > > less) on a million that you can guess them by just randomly typing
            > > them. So I need to know what is the value of x.[/color]
            >
            > OK, one in a million chance of successfully guessing 10,000 strings
            > equals 0.9986 chance of successfully guessing a single string:
            >
            > 0.9986 ^ 10000 = 8.23412E-07 ~ 1E-06 (one in a million)[/color]

            As I said in another branch of the thread, I wasn't clear enough: I
            meant 'one chance (or less) on a million that you can guess _any_ of
            them'.

            Anyway... Here's the code I wrote to generate my 10000 strings, just in
            case it's useful to somebody browsing the archives, someday. The
            random function is pretty much the same as the one you see on every php
            tutorial, but it uses mt_rand() instead of rand() as many of you have
            advised me. and I wrote a (very inefficient) dupe checker.
            Optimizations and ideas are welcome, but that's just for the fun of it:
            I'll generate these strings just once, so it doesn't matter if it takes
            one full minute, it will only be ran once. :-)

            <?php
            set_time_limit (600); // We need this because of the time-consuming
            // in_array()
            $values = array();
            function random_string() {
            $allowed_chars = "0123456789AZER TYUIOPQSDFGHJKL MWXCVBN";
            mt_srand((doubl e)microtime()*1 000000);
            $string = 'A'; // I put a control char at the start of my
            // password just in case I want to generate
            // a second, third,... series in the future.
            for($i = 0 ;$i <= 6; $i++) {
            $position = mt_rand()%36;
            $temp = substr($allowed _chars, $position, 1);
            $string .= $temp;
            }
            return $string;
            }

            // The odds are low, but it's possible that the same string
            // is generated twice. So I check if each new string found
            // isn't in the previous string found in the array, but it
            // slows down the script, which is not a problem in my
            // case, but I welcome optimizations ideas.

            // If a dupe is detected, I just decrement $i, which forces
            // the loop to loop once more for this value of $i.

            for ($i = 1 ; $i <= 10000; $i++) {
            if (!in_array($str ing,$values)) {
            $values[$i] = random_string() ;
            } else {
            $i--;
            }
            }
            echo '<pre>';
            print_r ($values);
            echo '</pre>';
            ?>

            JFLac

            Comment

            • Andy Hassall

              #7
              Re: Random strings of character and some stats

              On 3 May 2005 03:44:19 -0700, "Jean-François Lacrampe" <jflacrampe@gma il.com>
              wrote:
              [color=blue]
              >Well, keeping in mind my very 'intuitive' and weak knowledge of math,
              >I'd guess that the more strings you put in the list, the more chances
              >you have to guess one (any) of them. If for instance I had a list long
              >enough to contain all the possible combinations, the odds would be 1/1,
              >right? If you divide the list by two, the odds are 1/2. And so on.
              >So the number of items in the list seems to matter: that's how I came
              >with the 10000 * 1000000 thing (by doing lots of intermediate and
              >stupid steps on a sheet of paper).[/color]

              Ah, yes of course. OK, I agree with your maths, looks right.

              --
              Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
              <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

              Comment

              Working...