A big tree / hash problem, maybe a bug

Q&A's, tips, howto's
Locked
hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

A big tree / hash problem, maybe a bug

Post 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
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

Ormente
Posts: 23
Joined: Tue Aug 31, 2010 1:54 pm
Location: Near Mâcon, France

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

Post 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.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

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

hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

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

Post 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.
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

Post 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.

Locked