Random generator – PHP

  php

Q(Question):

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

A(Answer):

"Trythat" <do***@bother.org> wrote in message
news:4q********************************@4ax.com…

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.
<snip> 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.

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)&0x7fffffff);
Garp

A(Answer):

On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <ga***@no7.blueyonder.co.uk> wrote:

"Trythat" <do***@bother.org> wrote in message
news:4q********************************@4ax.com.. .

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.

<snip>

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.

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)&0x7fffffff);

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.

A(Answer):

"Trythat" <do***@bother.org> wrote in message
news:nn********************************@4ax.com…

On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <ga***@no7.blueyonder.co.uk>
wrote:
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.

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.

A(Answer):

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:nL********************@comcast.com…

"Trythat" <do***@bother.org> wrote in message
news:nn********************************@4ax.com…

On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <ga***@no7.blueyonder.co.uk> wrote:

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.

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.

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

A(Answer):

On Mon, 12 Apr 2004 23:33:22 GMT, "Garp" <ga***@no7.blueyonder.co.uk> wrote:

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:nL********************@comcast.com…

"Trythat" <do***@bother.org> wrote in message
news:nn********************************@4ax.com…

> On Mon, 12 Apr 2004 19:01:19 GMT, "Garp" <ga***@no7.blueyonder.co.uk>

wrote:

>
> 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.

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.

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.

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 :(?

A(Answer):

In article <6H*********************@news-text.cableinet.net>, "Garp"
<ga***@no7.blueyonder.co.uk> wrote:

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.

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.

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.

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]

LEAVE A COMMENT