Skip to content

Commit 030cdd1

Browse files
committed
Make max parameter inclusive in random method, fix #43.
Add $.radom(int max) method support.
1 parent e007592 commit 030cdd1

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,8 +1708,12 @@ public static <E> void times(final int count, final Function<E> function) {
17081708
}
17091709
}
17101710

1711-
public static int random(final int from, final int to) {
1712-
return new Random().nextInt(to - from) + from;
1711+
public static int random(final int min, final int max) {
1712+
return min + new Random().nextInt(max - min + 1);
1713+
}
1714+
1715+
public static int random(final int max) {
1716+
return new Random().nextInt(max + 1);
17131717
}
17141718

17151719
public static long now() {

src/test/java/com/github/underscore/UtilityTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015 Valentyn Kolesnikov
4+
* Copyright 2016 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -92,6 +92,27 @@ public void random() {
9292
assertTrue(result <= 100);
9393
}
9494

95+
/*
96+
_.random(0, 1);
97+
=> 0 or 1
98+
*/
99+
@Test
100+
public void random2() {
101+
int result = $.random(0, 1);
102+
assertTrue(result >= 0);
103+
assertTrue(result <= 1);
104+
}
105+
106+
/*
107+
_.random(0);
108+
=> 0
109+
*/
110+
@Test
111+
public void random3() {
112+
int result = $.random(0);
113+
assertEquals(0, result);
114+
}
115+
95116
/*
96117
_.now();
97118
=> 1392066795351

0 commit comments

Comments
 (0)