Random generator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Trythat

    Random generator

    This might seem a stupid question but here goes -

    I wrote a key generator for one of my programs in Visual Basic, so I recieve
    sales notification via email and then use my program to generate the key and
    send it via Email.

    Well after finding out about PHP, and that my host does PHP, I thought it would
    be quicker and more reliable to generate the keys on the server and send them
    out again either vai email or from the sales site.

    So I wrote the generator in PHP and surprisingly it works, or rather it works
    in a half assed fashion, I can generate keys, but they are not the same as the
    ones generated by my VB program.

    And here is my problem, I suspect its becuase I'm using a randomised Seed, and
    I'm thinking that VB and PHP Randomisers are not compatable.

    Am I right in my thoughts and if so, is there a way around it?
    Is the only answer not to use a randomiser? or how do people generate keys from
    the server when using other languages ie C++ etc?

    TIA
  • Garp

    #2
    Re: Random generator


    "Trythat" <don't@bother.o rg> wrote in message
    news:4qll70du7a qnuqr9p5rio1qse c1l7ht2pv@4ax.c om...[color=blue]
    > This might seem a stupid question but here goes -
    >
    > I wrote a key generator for one of my programs in Visual Basic, so I[/color]
    recieve[color=blue]
    > sales notification via email and then use my program to generate the key[/color]
    and[color=blue]
    > send it via Email.[/color]
    <snip>[color=blue]
    > And here is my problem, I suspect its becuase I'm using a randomised Seed,[/color]
    and[color=blue]
    > I'm thinking that VB and PHP Randomisers are not compatable.[/color]

    I should hope so.

    If you truly want the same number from the same seed, use the same algorithm
    in both.

    Try this one in both:
    (return x= (x*1103515245UL +12345UL)&0x7ff fffff);


    Garp


    Comment

    • Trythat

      #3
      Re: Random generator

      On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <garp7@no7.blue yonder.co.uk> wrote:
      [color=blue]
      >
      >"Trythat" <don't@bother.o rg> wrote in message
      >news:4qll70du7 aqnuqr9p5rio1qs ec1l7ht2pv@4ax. com...[color=green]
      >> This might seem a stupid question but here goes -
      >>
      >> I wrote a key generator for one of my programs in Visual Basic, so I[/color]
      >recieve[color=green]
      >> sales notification via email and then use my program to generate the key[/color]
      >and[color=green]
      >> send it via Email.[/color]
      ><snip>[color=green]
      >> And here is my problem, I suspect its becuase I'm using a randomised Seed,[/color]
      >and[color=green]
      >> I'm thinking that VB and PHP Randomisers are not compatable.[/color]
      >
      >I should hope so.
      >
      >If you truly want the same number from the same seed, use the same algorithm
      >in both.
      >
      >Try this one in both:
      >(return x= (x*1103515245UL +12345UL)&0x7ff fffff);
      >[/color]

      I am using the same algorithm, but using Random and as you say, they generate
      the random numbers differently, I've not time to now to try out your answer
      above, but what does it actually mean?

      They way I understood the VB and PHP randomiser for that matter, is that if I
      seed them, I will get a random number each time but its a psudo random number,
      in so much as each time I call it the random numbers are repeatable.

      But thinking about it, perhaps they're not as random as I thought, as I only
      run the program each time I get a registration, and the seed is always the
      same.

      This mathamatics is making making my head hurt, I think I'm going to bed and
      try and think this out a bit more.

      Thanks for your help though, if you've anything more to add, I'll take it with
      open arms.

      Comment

      • Chung Leong

        #4
        Re: Random generator

        "Trythat" <don't@bother.o rg> wrote in message
        news:nn4m70th9m p24bo3pedrii1oj eqfr4ha78@4ax.c om...[color=blue]
        > On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <garp7@no7.blue yonder.co.uk>[/color]
        wrote:[color=blue]
        >
        > I am using the same algorithm, but using Random and as you say, they[/color]
        generate[color=blue]
        > the random numbers differently, I've not time to now to try out your[/color]
        answer[color=blue]
        > above, but what does it actually mean?
        >
        > They way I understood the VB and PHP randomiser for that matter, is that[/color]
        if I[color=blue]
        > seed them, I will get a random number each time but its a psudo random[/color]
        number,[color=blue]
        > in so much as each time I call it the random numbers are repeatable.[/color]

        How do you know you're using the same pseudo-random algorithm? The rand
        function in PHP uses the C rand function, which may differ from C library to
        C library.


        Comment

        • Garp

          #5
          Re: Random generator


          "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
          news:nLmdnQ20oe-AvObdRVn-vw@comcast.com. ..[color=blue]
          > "Trythat" <don't@bother.o rg> wrote in message
          > news:nn4m70th9m p24bo3pedrii1oj eqfr4ha78@4ax.c om...[color=green]
          > > On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <garp7@no7.blue yonder.co.uk>[/color]
          > wrote:[color=green]
          > >
          > > I am using the same algorithm, but using Random and as you say, they[/color]
          > generate[color=green]
          > > the random numbers differently, I've not time to now to try out your[/color]
          > answer[color=green]
          > > above, but what does it actually mean?
          > >
          > > They way I understood the VB and PHP randomiser for that matter, is that[/color]
          > if I[color=green]
          > > seed them, I will get a random number each time but its a psudo random[/color]
          > number,[color=green]
          > > in so much as each time I call it the random numbers are repeatable.[/color]
          >
          > How do you know you're using the same pseudo-random algorithm? The rand
          > function in PHP uses the C rand function, which may differ from C library[/color]
          to[color=blue]
          > C library.[/color]

          Almost certainly.

          What the PRNG system does is calculate a seemingly random number based on a
          seed value, using an algorithm like the one I posted above. That will
          produce a predictable sequence from known seeds, but seed it from time() and
          it will be as random as you need.

          What I'm suggesting to the OP is that he doesn't rely on the built-ins
          because he's getting different results - so use a consistent algorithm on
          both. He could then store the seed in the database and get the same value
          from both systems, which is what he wanted.

          Garp


          Comment

          • Trythat

            #6
            Re: Random generator

            On Mon, 12 Apr 2004 23:33:22 GMT, "Garp" <garp7@no7.blue yonder.co.uk> wrote:
            [color=blue]
            >
            >"Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
            >news:nLmdnQ20o e-AvObdRVn-vw@comcast.com. ..[color=green]
            >> "Trythat" <don't@bother.o rg> wrote in message
            >> news:nn4m70th9m p24bo3pedrii1oj eqfr4ha78@4ax.c om...[color=darkred]
            >> > On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <garp7@no7.blue yonder.co.uk>[/color]
            >> wrote:[color=darkred]
            >> >
            >> > I am using the same algorithm, but using Random and as you say, they[/color]
            >> generate[color=darkred]
            >> > the random numbers differently, I've not time to now to try out your[/color]
            >> answer[color=darkred]
            >> > above, but what does it actually mean?
            >> >
            >> > They way I understood the VB and PHP randomiser for that matter, is that[/color]
            >> if I[color=darkred]
            >> > seed them, I will get a random number each time but its a psudo random[/color]
            >> number,[color=darkred]
            >> > in so much as each time I call it the random numbers are repeatable.[/color]
            >>
            >> How do you know you're using the same pseudo-random algorithm? The rand
            >> function in PHP uses the C rand function, which may differ from C library[/color]
            >to[color=green]
            >> C library.[/color]
            >
            >Almost certainly.
            >
            >What the PRNG system does is calculate a seemingly random number based on a
            >seed value, using an algorithm like the one I posted above. That will
            >produce a predictable sequence from known seeds, but seed it from time() and
            >it will be as random as you need.
            >
            >What I'm suggesting to the OP is that he doesn't rely on the built-ins
            >because he's getting different results - so use a consistent algorithm on
            >both. He could then store the seed in the database and get the same value
            >from both systems, which is what he wanted.[/color]

            Thank you both for your help, I think for the time being I'm going to leave
            this one program as it is.

            But I plan on converting the main program to Java and running it on the Palm
            and Pocket PC, so I can use basicly the same routine but without using Random
            from either Java or PHP.

            Garp is there a page I can find out a bit more info about the algorithm you
            posted, as I really didn't understand it :(?

            Comment

            • Sandman

              #7
              Re: Random generator

              In article <6HFec.2251$i67 .22112301@news-text.cableinet. net>, "Garp"
              <garp7@no7.blue yonder.co.uk> wrote:
              [color=blue][color=green][color=darkred]
              >>> I am using the same algorithm, but using Random and as you say, they
              >>> generate the random numbers differently, I've not time to now to try
              >>> out your answer above, but what does it actually mean?
              >>>
              >>> They way I understood the VB and PHP randomiser for that matter, is
              >>> that if I seed them, I will get a random number each time but its a
              >>> psudo random number, in so much as each time I call it the random
              >>> numbers are repeatable.[/color]
              >>
              >> How do you know you're using the same pseudo-random algorithm? The
              >> rand function in PHP uses the C rand function, which may differ from
              >> C library to C library.[/color]
              >
              > Almost certainly.
              >
              > What the PRNG system does is calculate a seemingly random number based
              > on a seed value, using an algorithm like the one I posted above. That
              > will produce a predictable sequence from known seeds, but seed it from
              > time() and it will be as random as you need.[/color]

              I suggest using microtime() instead, if you may want to access a random value
              more than once in a second.



              function getmicrotime(){
              list($usec, $sec) = explode(" ",microtime ());
              return ((float)$usec + (float)$sec);
              }

              --
              Sandman[.net]

              Comment

              Working...