- Subject: [slang-users] Re: generating random characters
- From: Troy Piggins <troy_slang@xxxxxxxxx>
- Date: Wed, 13 Feb 2008 17:09:37 +1000
* John E. Davis is quoted & my replies are inline below :
> Michael Noble <mnoble@xxxxxxxxxxxxx> wrote:
> > private variable rseed = getpid();
> > private define random()
> >{
> > rseed = (rseed * 0x5DEECEDL + 0xB) & ((1 shl 30) - 1);
> > (rseed shr 6) / ((1 shl 24) * 1.0);
> >}
Isn't there a problem with this? Because I'm calling this macro from within
slrn, rseed is always giving the pid of the same slrn and so is the same
number?
> Another one that is ok for the purposes Troy has in mind is:
>
> private variable Random;
> define srandom (r)
> {
> Random = r;
> }
> srandom (_time() * getpid());
Perhaps I should have explained more detail about my reasons for wanting random
numbers. I have a macro that uses _time to generate the left hand side of my
Message-IDs like this 20080213165912@xxxxxxxxxxxxxxxx :
#v+
define create_msg_id()
{
variable sd= "usenet";
variable hostname= "piggo.com";
variable tm= localtime( _time ());
return sprintf ("%d%02d%02d%02d%02d%02d@%s.%s",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
sd, hostname);
}
#v-
I was thinking this was unique enough to serve as M-IDs, but a suggestion from
Peter J Ross and also a recent experience I had where my system clock jumped
forward in the future made me realise that it could be possible to have 2 M-IDs
created the same. You'd have to be really, really unlucky to get that
considering it would have to be the exact same second, but ...
PJR suggest inserting 3 random chars after the numbers like:
20080213165912.xyz@xxxxxxxxxxxxxxxx
And that's where my little quest here began.
So coming back to your suggestion - wouldn't the getpid always generate the
same pid if called from within the same slrn process? And if we are trying to
prevent a possible mishap with the time-generated LHS of M-ID anyway, I think
it'd be dangerous to use _time again for this random part?
> define random ()
> {
> Random = (Random*69069U + 1013904243U)&0xFFFFFFFFU;
> return Random;
> }
> define urandom ()
> {
> return random()/4294967296.0;
> }
>
> Here, urandom() returns a random number between 0 and 1, whereas
> random returns a random unsigned integer between 0 and 0xFFFFFFFF.
I'd be after a random number between 0 and 999 for my purposes :)
> FWIW, the next slang release (2.1.4) will include a random number
> module. The module will feature a generator that according to the
> "dieharder" battery of tests has slightly better statistical
> properties than the Mersenne Twister and is about as fast. This
> module is not yet in the svn repository but will appear on the next
> update.
So it will be part of "standard" s-lang 2.1.4? Any idea how far away that
release is? Or with all your time spent at the moment on the slrn-mafia
chasing a release, am I asking a bit much? :)
--
Troy Piggins | http://piggo.com/~troy
"At my house, we call them 'Uh-Oh's.'"
[2008 date index]
[2008 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]