slang-users mailing list

[2008 Date Index] [2008 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Re: generating random characters


Troy Piggins <troy_slang@xxxxxxxxx> 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?

Each time the random function is called, rseed will change.  Its
initial value is that of the pid.

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

The idea is that you call random each time you want a pseudo random
number.  getpid is only used once to initialize the value.

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

You have to seed the random number generator is some way.  Using the
current time and process id are typical ways to do this.  And this
only needs to happen once.

>>   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 :)

If you use the above definition of random, then use:
  
     r = random() mod 1000;

>> FWIW, the next slang release (2.1.4) will include a random number
>> module.  The module will feature a generator that according to the
[...]
> 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? :)

It will be a standard module in slang-2.1.4.  I do not have any release
plans at this time-- perhaps sometime in the spring.  I plan to make
that my last release of the 2.1 series.

I hope this clears up your confusion with the random number seed.
Thanks,
--John




[2008 date index] [2008 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]