Skip to content

inconsistent behaviour of nextInt and nextLong #1395

@asolntsev

Description

@asolntsev

Describe the bug
Methods nextInt and nextLong have inconsistent behaviour. The "max" parameter is exclusive for most of them, but inclusive for one of them.

  • nextInt(3): [0, 1, 2] - exclusive
  • nextInt(2, 4): [2, 3, 4] - INCLUSIVE
  • nextLong(3): [0, 1, 2] - exclusive
  • nextLong(2, 4): [2, 3] - exclusive
  • nextDouble(2, 3) - exclusive

To Reproduce

    @RepeatedTest(100)
    void exclusive() {
        assertThat(randomService.nextInt(3)).isLessThan(3); // ok
        assertThat(randomService.nextInt(1, 3)).isLessThan(3); // fails because 3==3
        assertThat(randomService.nextLong(3)).isLessThan(3); //ok
        assertThat(randomService.nextLong(1, 3)).isLessThan(3); // ok
        assertThat(randomService.nextDouble(2, 3)).isLessThan(3); // ok
    }

Expected behavior
All these methods should have the same behaviour: either inclusive or exclusive.
I assume exclusive is better because

  1. most of them are already exclusive
  2. Standard java methods java.lang.Random.nextInt(n), java.lang.Random.nextLong(n) are exclusive.

@bodiam @kingthorin What do you choose: inclusive or exclusive?

Versions:

  • Faker Version: 1.0 ... 2.4.0

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions