Skip to content

Commit 7cbd28a

Browse files
authored
Fix: fix dice number generator in JS examples (#5565)
1 parent e24b5cf commit 7cbd28a

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

content/en/docs/languages/js/getting-started/nodejs.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const PORT: number = parseInt(process.env.PORT || '8080');
8080
const app: Express = express();
8181

8282
function getRandomNumber(min: number, max: number) {
83-
return Math.floor(Math.random() * (max - min) + min);
83+
return Math.floor(Math.random() * (max - min + 1) + min);
8484
}
8585

8686
app.get('/rolldice', (req, res) => {
@@ -102,7 +102,7 @@ const PORT = parseInt(process.env.PORT || '8080');
102102
const app = express();
103103

104104
function getRandomNumber(min, max) {
105-
return Math.floor(Math.random() * (max - min) + min);
105+
return Math.floor(Math.random() * (max - min + 1) + min);
106106
}
107107

108108
app.get('/rolldice', (req, res) => {

content/en/docs/languages/js/instrumentation.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ TypeScript) and add the following code to it:
7575
```ts
7676
/*dice.ts*/
7777
function rollOnce(min: number, max: number) {
78-
return Math.floor(Math.random() * (max - min) + min);
78+
return Math.floor(Math.random() * (max - min + 1) + min);
7979
}
8080

8181
export function rollTheDice(rolls: number, min: number, max: number) {
@@ -92,7 +92,7 @@ export function rollTheDice(rolls: number, min: number, max: number) {
9292
```js
9393
/*dice.js*/
9494
function rollOnce(min, max) {
95-
return Math.floor(Math.random() * (max - min) + min);
95+
return Math.floor(Math.random() * (max - min + 1) + min);
9696
}
9797

9898
function rollTheDice(rolls, min, max) {
@@ -558,7 +558,7 @@ import { trace } from '@opentelemetry/api';
558558
const tracer = trace.getTracer('dice-lib');
559559

560560
function rollOnce(min: number, max: number) {
561-
return Math.floor(Math.random() * (max - min) + min);
561+
return Math.floor(Math.random() * (max - min + 1) + min);
562562
}
563563

564564
export function rollTheDice(rolls: number, min: number, max: number) {
@@ -579,7 +579,7 @@ const { trace } = require('@opentelemetry/api');
579579
const tracer = trace.getTracer('dice-lib');
580580

581581
function rollOnce(min, max) {
582-
return Math.floor(Math.random() * (max - min) + min);
582+
return Math.floor(Math.random() * (max - min + 1) + min);
583583
}
584584

585585
function rollTheDice(rolls, min, max) {
@@ -707,7 +707,7 @@ nested operation. The following sample creates a nested span that tracks
707707
```ts
708708
function rollOnce(i: number, min: number, max: number) {
709709
return tracer.startActiveSpan(`rollOnce:${i}`, (span: Span) => {
710-
const result = Math.floor(Math.random() * (max - min) + min);
710+
const result = Math.floor(Math.random() * (max - min + 1) + min);
711711
span.end();
712712
return result;
713713
});
@@ -732,7 +732,7 @@ export function rollTheDice(rolls: number, min: number, max: number) {
732732
```js
733733
function rollOnce(i, min, max) {
734734
return tracer.startActiveSpan(`rollOnce:${i}`, (span) => {
735-
const result = Math.floor(Math.random() * (max - min) + min);
735+
const result = Math.floor(Math.random() * (max - min + 1) + min);
736736
span.end();
737737
return result;
738738
});
@@ -849,7 +849,7 @@ information about the current operation that it's tracking.
849849
```ts
850850
function rollOnce(i: number, min: number, max: number) {
851851
return tracer.startActiveSpan(`rollOnce:${i}`, (span: Span) => {
852-
const result = Math.floor(Math.random() * (max - min) + min);
852+
const result = Math.floor(Math.random() * (max - min + 1) + min);
853853

854854
// Add an attribute to the span
855855
span.setAttribute('dicelib.rolled', result.toString());
@@ -865,7 +865,7 @@ function rollOnce(i: number, min: number, max: number) {
865865
```js
866866
function rollOnce(i, min, max) {
867867
return tracer.startActiveSpan(`rollOnce:${i}`, (span) => {
868-
const result = Math.floor(Math.random() * (max - min) + min);
868+
const result = Math.floor(Math.random() * (max - min + 1) + min);
869869

870870
// Add an attribute to the span
871871
span.setAttribute('dicelib.rolled', result.toString());
@@ -1466,7 +1466,7 @@ const tracer = trace.getTracer('dice-lib');
14661466
const meter = metrics.getMeter('dice-lib');
14671467

14681468
function rollOnce(min: number, max: number) {
1469-
return Math.floor(Math.random() * (max - min) + min);
1469+
return Math.floor(Math.random() * (max - min + 1) + min);
14701470
}
14711471

14721472
export function rollTheDice(rolls: number, min: number, max: number) {
@@ -1488,7 +1488,7 @@ const tracer = trace.getTracer('dice-lib');
14881488
const meter = metrics.getMeter('dice-lib');
14891489

14901490
function rollOnce(min, max) {
1491-
return Math.floor(Math.random() * (max - min) + min);
1491+
return Math.floor(Math.random() * (max - min + 1) + min);
14921492
}
14931493

14941494
function rollTheDice(rolls, min, max) {
@@ -1523,7 +1523,7 @@ const counter = meter.createCounter('dice-lib.rolls.counter');
15231523

15241524
function rollOnce(min: number, max: number) {
15251525
counter.add(1);
1526-
return Math.floor(Math.random() * (max - min) + min);
1526+
return Math.floor(Math.random() * (max - min + 1) + min);
15271527
}
15281528
```
15291529

@@ -1535,7 +1535,7 @@ const counter = meter.createCounter('dice-lib.rolls.counter');
15351535

15361536
function rollOnce(min, max) {
15371537
counter.add(1);
1538-
return Math.floor(Math.random() * (max - min) + min);
1538+
return Math.floor(Math.random() * (max - min + 1) + min);
15391539
}
15401540
```
15411541

0 commit comments

Comments
 (0)