0

code is this:

Long indx = getComboIndex(drawPosCombo,numsize,picksize); //[resolves to the value 1046]
Long cycles = 0l;
Long comboSize = getComboSize(numsize,picksize);  //[resolves to the value 10626]
Long testIndex = ThreadLocalRandom.current().nextLong(0, comboSize);

while(testIndex != indx){
    testIndex = ThreadLocalRandom.current().nextLong(0, comboSize);
    cycles += 1;
}

I'm expecting this to run the while loop until I get a match between indx and testIndex and then continue on. But, it never leaves the loop :)

I dropped cycles in there to test how many attempts I had tried... after 21 Billion! attempts, it still cannot match the indx value of 1046 from the range of (0,10626). I stare at it and stare at it but my brain fails to see the issue.. please help :)

debugger shows that it faithfully is creating long values in range. It boggles my mind that a random generator would not match after this many tries.

andrewJames
  • 19,570
  • 8
  • 19
  • 51

1 Answers1

0

Long cycles = 0l;

This is incorrect. You want long cycles = 0L;. The capital L is just because lowercase-l is obviously extremely unreadable (looks like a 1), but that's just a style thing. The long vs Long, that's.. quite a gigantic difference. Long is a wrapper object containing a long value.

a == b, where a and b are Long objects, checks if they are the exact same wrapper object, not if the values they represent are identical. e.g.:

Long a = Long.parseLong("1234");
Long b = Long.parseLong("1234");
System.out.println(a == b); // prints false

try it.

There does not appear to be any reason for why you are using the wrapper here. Simply.. don't. Always use long unless you really, really know what you are doing. The one place where Long has to be used is in generics, e.g. if you have a new ArrayList<Long>(), that has to be capital-L long, but then that also belies what is happening: You have these wrappers involved.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • @Unmitigated of course. The point is to illustrate how `Long` is not `long`, I do not understand your comment. The code will print false whether you use `parseLong` or `valueOf` - _that was the point of the example_. – rzwitserloot Apr 10 '23 at 21:14