Page 1 of 1

A big tree / hash problem, maybe a bug

Posted: Fri Sep 03, 2010 4:22 pm
by hilti
I'm expecting some problems in creating big hashes, when running the following code.

Code: Select all

(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

Re: A big tree / hash problem, maybe a bug

Posted: Fri Sep 03, 2010 5:50 pm
by Ormente
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.

Re: A big tree / hash problem, maybe a bug

Posted: Fri Sep 03, 2010 6:07 pm
by Lutz
Ormente is correct: the random function does not guarantee unique results. For that use the 'uuid' function, it guarantees true unique hash-able ids.

Code: Select all

> (define BigTree:BigTree)
nil
> (dotimes (i 1000000) (BigTree (uuid) i))
999999
> (length (symbols BigTree))
1000001
> 

Re: A big tree / hash problem, maybe a bug

Posted: Sat Sep 04, 2010 10:29 am
by hilti
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.

Re: A big tree / hash problem, maybe a bug

Posted: Sat Sep 04, 2010 12:31 pm
by Lutz
…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.