Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 134632c

Browse files
committed
Add double randomizer
1 parent 5d2afad commit 134632c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

languages/java/src/main/java/org/algorithm_visualizer/tracers/Randomize.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@ public java.lang.Integer create() {
4141
}
4242
}
4343

44+
public static class Double extends Randomizer {
45+
@Override
46+
protected Class getType() {
47+
return java.lang.Double.class;
48+
}
49+
50+
private double _min;
51+
private double _max;
52+
53+
public Double(double min, double max) {
54+
_min = min;
55+
_max = max;
56+
}
57+
58+
public Double(double min) {
59+
this(min, 1);
60+
}
61+
62+
public Double() {
63+
this(0);
64+
}
65+
66+
@Override
67+
public java.lang.Double create() {
68+
return random.nextDouble() * (_max - _min) + _min;
69+
}
70+
}
71+
4472
public static class String extends Randomizer {
4573
@Override
4674
protected Class getType() {

languages/js/src/Randomize.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ class Integer extends Randomizer {
1212
}
1313

1414
create() {
15-
return (Math.random() * (this._max - this._min + 1) | 0) + this._min;
15+
return Math.random() * (this._max - this._min + 1) + this._min | 0;
16+
}
17+
}
18+
19+
class Double extends Randomizer {
20+
constructor(min = 0, max = 1) {
21+
super();
22+
this._min = min;
23+
this._max = max;
24+
}
25+
26+
create() {
27+
return Math.random() * (this._max - this._min) + this._min;
1628
}
1729
}
1830

specs/randomizers/Double.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
name: 'Randomize.Double',
3+
description: 'Create a random real number.',
4+
methods: [{
5+
name: 'Randomize.Double',
6+
description: 'The real number would be between `min` (inclusive) and `max` (exclusive).',
7+
return: 'new',
8+
arguments: [
9+
{ name: 'min', type: 'double', default: '0' },
10+
{ name: 'max', type: 'double', default: '1' },
11+
],
12+
}, {
13+
name: 'create',
14+
description: 'Create a random real number.',
15+
return: 'double',
16+
arguments: [],
17+
}],
18+
};

0 commit comments

Comments
 (0)