[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Random (Was: Cannot run KDE snapshot :()
From:       Bo Thorsen <gobo () imada ! sdu ! dk>
Date:       1999-12-10 9:41:26
[Download RAW message or body]

> > What has the clock to do with the random seed? Nothing inspite
> > that most people abuse the clock for getting a semi random start
> > seed. The problem comes if you use the KTempFile constructor
> > more than once in a second, it will init the seed with the same
> > number resulting in the same output. 
> 
> Hm interesting. That could cause a problem when you need more than 3
> tempfiles within a second.
> 
> Can't we init the random seed in KApplication and make it a policy not
> to touch the random seed? If you need reproducable sequences just write
> your own sequence generator. (That's not too difficult, I just need to
> dig out my math books.)
> 
> We could provide:
> 
> int KApplication::random() // Generate a truly unpredictable number.
> 
> and 
> class KSequence // Generate a random sequence 
> {
> public:
>   KSequence(int seed=0);
>   int number();
> private:
>   ....
> }
> 
> KSequence(N) initialize the sequence with N for N >0 and with
> kapp->random() for N == 0.

In java I have always solved this problem by using the this pointer as
seed, sometimes you would multiply/add/something the current time, to
facilitate multiple instances of java.lang.Random made with more than
20ms between. This will only be a problem, if one object sets a seed - in
java instantiates Random - several times. In this case you can make dummy
objects and use their pointers as seeds. It should be a fairly good hack
for creating random numbers.

If you want to make the class KSequence, I think you should rename it to
KRandom. This could then have static to give you an integer, float etc.
and there would be no distinction between having one random number and
having a sequence of random numbers. I think it will be a cause of
confusion if there is this distinction and even more so, if the random
method is not even in the same class. It strikes me as very bad design.

But I really enjoy having the Random class in java. I think it would be
great to have something like it in Kde.

--

Bo Thorsen
gobo@imada.sdu.dk
Lahnsgade 31, st.
DK-5000 Odense C
Tlf: +45 66 11 83 85

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic