newLISP Fan Club

Forum => newLISP in the real world => Topic started by: hilti on September 03, 2010, 09:22:27 AM

Title: A big tree / hash problem, maybe a bug
Post by: hilti on September 03, 2010, 09:22:27 AM
I'm expecting some problems in creating big hashes, when running the following code.



(define Bigtree:Bigtree)
(new Tree 'Bigtree)

(define (fill-tree)
(for (x 1 20000)
(Bigtree (string (random 10 5)) (string (random 5 5)))
)
)

(define (show-size name)
(length name)
)

(define (show-tree name)
name
)

(fill-tree)

(show-size (Bigtree))


The code creates a hash and inserts 20000 random values into it.



On the first iteration (show-size) returns 20000



After running (fill-tree) another round the function (show-size) returns 39998



Doing this again will return 59995.



Am I doing something wrong with the hashes?



Cheers

Hilti
Title: Re: A big tree / hash problem, maybe a bug
Post by: Ormente on September 03, 2010, 10:50:34 AM
I'm a noob, but i would say that "(random 10 5)" is random but not unique, so it can happens that you get the same key twice or more.
Title: Re: A big tree / hash problem, maybe a bug
Post by: Lutz on September 03, 2010, 11:07:35 AM
Ormente is correct: the random function does not guarantee unique results. For that use the 'uuid' function, it guarantees true unique hash-able ids.


> (define BigTree:BigTree)
nil
> (dotimes (i 1000000) (BigTree (uuid) i))
999999
> (length (symbols BigTree))
1000001
>
Title: Re: A big tree / hash problem, maybe a bug
Post by: hilti on September 04, 2010, 03:29:14 AM
Thank You Lutz! That works like a charm.



I didn't thought about the fact, that random is in this case not unique enough, because the way elements were missing was reproducible and regular.
Title: Re: A big tree / hash problem, maybe a bug
Post by: Lutz on September 04, 2010, 05:31:59 AM
Quote...because the way elements were missing was reproducible and regular.

Each newLISP process starts out with the same pseudo random sequence. This repeatability of the random generator is needed and useful for debugging purposes. You can use the 'seed' function, to make a different random sequence.