Problem? Or my stupidity?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem? Or my stupidity?

darel.finkbeiner
Ok,  so I have code that looks like this at the top of the program:

math.randomseed( os.time() )

Then I have this code in the event loop every time I tap a button:

screen.moveto( 0, 52 )
print( math.random( 10 ) )

However, the sequence of numbers ( 1, 7, 8, etc. etc. ) is the same every
time.  Am I doing something wrong or stupid?  Is this supposed to work and
is just a quirk or bug?

Oh yeah, Plua 2.0b7, Palm Lifedrive ( OS v5.4.8 )


[Non-text portions of this message have been removed]

Reply | Threaded
Open this post in threaded view
|

Re: Problem? Or my stupidity?

abm0000abm-2
math.randomseed( os.time() )

print( math.random( 10 ) )
print( math.random( 10 ) )
print( math.random( 10 ) )
print( math.random( 10 ) )
print( math.random( 10 ) )

gui.event()


Gives me 9,3,3,4,8 on one launch, 7,2,4,10,8 on another, etc. Seems to
work for me. I'm running in the Palm OS Simulator, though.

Reply | Threaded
Open this post in threaded view
|

Re: Problem? Or my stupidity?

migueletto
In reply to this post by darel.finkbeiner
Hi,

> math.randomseed( os.time() )
>
> Then I have this code in the event loop every time I tap a button:
>
> screen.moveto( 0, 52 )
> print( math.random( 10 ) )
>
> However, the sequence of numbers ( 1, 7, 8, etc. etc. ) is the same
every
> time.  Am I doing something wrong or stupid?  Is this supposed to
work and
> is just a quirk or bug?
>
> Oh yeah, Plua 2.0b7, Palm Lifedrive ( OS v5.4.8 )

It is a bug. math.randomseed() does not accept large numbers. For now,
try a smaller seed.

Regards,
Marcio.

Reply | Threaded
Open this post in threaded view
|

re: Re: Problem? Or my stupidity?

AndyS
In reply to this post by darel.finkbeiner
Also, math.randomseed(0) doesn't work usefully-it seems to ignore a seed of 0.

E.g. This program:
math.randomseed(10)
math.randomseed(0)
print(math.random(10),math.random(10),math.random(10))
Just produces 4 4 3 everytime it is run...

So, we need to do a math.mod(systime, x).

What is the value of x - i.e. What is the largest number that randomseed can manage?

>It is a bug. math.randomseed() does not accept large numbers. For now,
>try a smaller seed.
>
>Regards,
>Marcio.

Reply | Threaded
Open this post in threaded view
|

Re: Problem? Or my stupidity?

migueletto
Hi,

> Also, math.randomseed(0) doesn't work usefully-it seems to ignore a
seed of 0.
>
> E.g. This program:
> math.randomseed(10)
> math.randomseed(0)
> print(math.random(10),math.random(10),math.random(10))
> Just produces 4 4 3 everytime it is run...

This is because math.randomseed() uses PalmOS API SysRandom(). In this
function a zero argument means: get the next random value in the
sequence started by the previous non-zero seed, which is your example
is 10.
 
> What is the value of x - i.e. What is the largest number that
randomseed can manage?

The largest 32-bit signed positive number, or (2^31)-1.

Regards,
Marcio.