c# - Join the HashCode magic -


am started testing hash function on uniqueness of generated hashcodes algorithm. , wrote next text class test when same hashcode generated.

class program {     static void main(string[] args)     {         var hashes = new list<int>();         (int = 0; < 100000; i++)         {             var vol = new volume();             var code = vol.gethashcode();             if (!hashes.contains(code))             {                 hashes.add(code);             }             else             {                 console.writeline("same hash code generated on {0} retry", hashes.count());             }         }     } }  public class volume {     public guid driverid = guid.newguid();     public guid computerid = guid.newguid();     public int size;     public ulong versionnumber;     public int hashcode;     public static ulong curdriverepochnumber;     public static random randomf = new random();      public volume()     {         size = randomf.next(1000000, 1200000);         curdriverepochnumber ++;         versionnumber = curdriverepochnumber;         hashcode = gethashcodeinternal();     }      public int gethashcodeinternal()     {         unchecked         {             var 1 = driverid.gethashcode() + computerid.gethashcode() * 22;             var 2 = (ulong)size + versionnumber;             var result = 1 ^ (int)two;             return result;         }     }   } 

guids fields driverid, computerid , int size random. assumed @ time generate same hash-code. know break work big collections. magic in fact retry number when duplicated hash code generated same! run sample code several time , got near same result: firs run duplicate on 10170 retry, second on 7628, third 7628 , again , again on 7628. times got little bit others results. bu in cases on 7628.

it has no explanations me. error in . net random generator or what?


thanks all. clear bug in code (matthew watson). had call gethashcodeintelrnal() , not gethashcode(). best gethashcode unique results gave me:

    public int gethashcodeinternal()     {         unchecked         {             var 1 = driverid.gethashcode() + computerid.gethashcode();             var 2 = ((ulong)size) + versionnumber;             var result = 1 ^ (int)two << 32;             return result;         }     }  

bu still on near 140 000 give same code... think not because ve have collections near 10 000...

if change console.writeline() print volume.size so:

console.writeline("same hash code generated on {0} retry ({1})", hashes.count, vol.size); 

you see although hashes.count same first collision, vol.size different.

this seems rule out random number generator causing issue - looks strange property of gethashcodeinternal().

closer inspection reveals calling wrong hash code function.

this line: var code = vol.gethashcode();

should be: var code = vol.hashcode;

try instead! because @ moment calling default .net gethashcode() not doing want @ all.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -