This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that's why it rounded down.
Præcisionstab kan forårsage både stigning og fald i et tal. I dette særlige tilfælde bliver tallet en smule mindre, derfor blev det afrundet nedad.
Note that `63.5` has no precision loss at all. That's because the decimal part `0.5` is actually `1/2`. Fractions divided by powers of `2` are exactly represented in the binary system, now we can round it:
Bemærk at `63.5` slet ikke har noget præcisionstab. Det skyldes, at decimaldelen `0.5` faktisk er `1/2`. Brøker divideret med potenser af `2` er nøjagtigt repræsenteret i det binære system, nu kan vi runde det korrekt:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The solution is a little bit more intricate that it could be because we need to handle `null`/empty lines.
Løsningen er lidt mere indviklet, end den kunne være, fordi vi skal håndtere `null`/tomme linjer.
So we actually accept the input until it is a "regular number". Both `null` (cancel) and empty line also fit that condition, because in numeric form they are `0`.
Så vi bliver ved med at acceptere input, indtil det er et "almindeligt tal". Både `null` (annuller) og tomme linjer passer også til den betingelse, fordi de i numerisk form er `0`.
After we stopped, we need to treat `null` and empty line specially (return `null`), because converting them to a number would return `0`.
Efter vi er stoppet, skal vi behandle `null` og tomme linjer specielt (returnere `null`), fordi konvertering til et tal ville returnere `0`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
Den simpleste, men forkerte løsning ville være at generere en værdi fra `min` til `max` og runde den:
```js run
function randomInteger(min, max) {
Expand All
@@ -11,28 +11,28 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```
The function works, but it is incorrect. The probability to get edge values `min` and `max` is two times less than any other.
Funktionen virker, men den er forkert. Sandsynligheden for at få kantværdierne `min` og `max` er to gange mindre end for alle andre.
If you run the example above many times, you would easily see that `2` appears the most often.
Hvis du kører eksemplet ovenfor mange gange, vil du nemt se, at `2` optræder oftest.
That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
Det sker, fordi `Math.round()` får tilfældige tal fra intervallet `1..3` og runder dem som følger:
```js no-beautify
values from 1 ... to 1.4999999999 become 1
values from 1.5 ... to 2.4999999999 become 2
values from 2.5 ... to 2.9999999999 become 3
```
Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
Nu kan vi tydeligt se, at `1` får halvt så mange værdier som `2`. Det samme gælder for `3`.
# The correct solution
# Den korrekte løsning
There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 3.5`, thus adding the required probabilities to the edges:
Der er mange korrekte løsninger på opgaven. En af dem er at justere intervalgrænserne. For at sikre de samme intervaller kan vi generere værdier fra `0.5 til 3.5`, og dermed tilføje de nødvendige sandsynligheder til kanterne:
```js run
*!*
function randomInteger(min, max) {
// now rand is from (min-0.5) to (max+0.5)
// Nu er rand fra (min-0.5) til (max+0.5)
let rand = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rand);
}
Expand All
@@ -41,12 +41,12 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```
An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
En alternativ måde kunne være at bruge `Math.floor` for et tilfældigt tal fra `min` til `max+1`:
```js run
*!*
function randomInteger(min, max) {
// here rand is from min to (max+1)
// her er rand fra min til (max+1)
let rand = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}
Expand All
@@ -55,12 +55,12 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```
Now all intervals are mapped this way:
Nu er alle intervaller mappet på denne måde:
```js no-beautify
values from 1 ... to 1.9999999999 become 1
values from 2 ... to 2.9999999999 become 2
values from 3 ... to 3.9999999999 become 3
```
All intervals have the same length, making the final distribution uniform.
Alle intervaller har samme længde, hvilket gør den endelige fordeling ensartet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create a function `randomInteger(min, max)` that generates a random *integer* number from `min` to `max` including both `min` and `max` as possible values.
Skriv en funktion `randomInteger(min, max)` der genererer et tilfældigt *heltal* fra `min` til `max` inklusive både `min` og `max` som mulige værdier.
Any number from the interval `min..max` must appear with the same probability.
Enhver værdi fra intervallet `min..max` skal have samme sandsynlighed.
Examples of its work:
Eksempler på hvordan den skal virke:
```js
alert( randomInteger(1, 5) ); // 1
alert( randomInteger(1, 5) ); // 3
alert( randomInteger(1, 5) ); // 5
```
You can use the solution of the [previous task](info:task/random-min-max) as the base.
Du kan bruge løsningen fra [forrige opgave](info:task/random-min-max) som base.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Numbers #234
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Numbers #234
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Uh oh!
There was an error while loading. Please reload this page.