Forum Discussion

ikerstges's avatar
ikerstges
New Contributor
15 years ago

Generate positive long integer?

Hi,

In some of my testcases, I need a random long integer. I've found these Groovy lines to provide me with such:

Random random = new Random();
long randomVal = random.nextLong();

Those two lines return positive AND negative values, but I'd need the number to be always positive.

Any tips or advise would be greatly appreciated!
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Not sure how it would affect "randomness" but what about taking the absolute value?

    [tt:3e5goql9]long randomVal = Math.abs(random.nextLong());[/tt:3e5goql9]