Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion concepts/randomness/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"blurb": "The System.Random class can be used to generate pseudo-random integral or floating-point numbers.",
"authors": ["mikedamay"],
"contributors": ["ErikSchierboom"]
"contributors": [
"ErikSchierboom",
"yrahcaz7"
]
}
30 changes: 19 additions & 11 deletions concepts/randomness/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@ In C# applications randomness is generally implemented using the `System.Random`

This [article][system-random] is an excellent introduction to the subject.

When becoming familiar with a library class it is always worth studying the [documentation][system-random] for properties and methods and their overloads.
When becoming familiar with a library class, it is always worth studying the [documentation][system-random] for properties and methods and their overloads.

The coding exercise highlighted a number of issues:
The coding exercise highlighted a number of aspects:

- The random number generator does not require a [seed][random-seedless].
- `Random.Next()` can generate a range of integers
- `Random.Double()` can generate a double between 0 and 1
- The upper bounds in `Next...()`, `Double()` and `Sample()` are **exclusive** so that `Random.Next(1,19)` will yield values from 1 to 18 but not 19.
- Once you have your random number you can do what you like with it
- `Random.Next()` can generate a range of integers.
- `Random.NextDouble()` can generate a double between 0 and 1.
- The upper bounds in `Next()`, `NextDouble()`, and `Sample()` are **exclusive** so that `Random.Next(1,19)` will yield values from 1 to 18 but not 19.
- Once you have your random number, you can do what you like with it.

There are 3 patterns for implementing the last point:

- You can get the random number and manipulate it. In the case of the coding exercise this would consist of calling `Random.NextDouble()` and multiplying by 100. This [piece][random-use-case-array] discusses making random selections from an array.
- You can get the random number and manipulate it.
In the case of the coding exercise, this would consist of calling `Random.NextDouble()` and multiplying by 100.
This [piece][random-use-case-array] discusses making random selections from an array.
- You can inherit from `System.Random` and override the `Sample()` method.
- You can encapsulate an instance of `System.Random` as a member of a class of your own, for example [to generate Booleans][random-use-cases].

The numbers generated by `Random` are not guaranteed to be unique.

It is [recommended][random-thread-safety] that you instantiate `System.Random` as a static member. But, note that it is not thread safe.
It is [recommended][random-thread-safety] that you instantiate `System.Random` as a static member.
But, note that it is not thread safe.

You may see documentation discouraging the use of seedless constructors. This mostly applies to .NET Framework rather than .NET Core (which is what _Exercism_ uses). For the full story read [this][random-multiple-instantiations].
You may see documentation discouraging the use of seedless constructors.
This mostly applies to .NET Framework rather than .NET Core (which is what _Exercism_ uses).
For the full story read [this][random-multiple-instantiations].

You are advised not to use `System.Random` for crypto or security. See this [provider][crypto-provider] and this [number generator][crypto-rng].
You are advised not to use `System.Random` for crypto or security.
See this [provider][crypto-provider] and this [number generator][crypto-rng].

## Thread safety

When applied in the context of library APIs "not thread safe" is simply shorthand for saying that, if you are likely to access instances of the class through multiple concurrent [threads][threading], you should provide your own thread synchronization mechanisms or, in the case of collections, look at the possibility of using a concurrent version of the class. In the absence of these precautions, [race conditions][so-race-conditions] are likely to occur. If your code base does not use multiple threads, and is not likely to, then this warning is of little concern.
When applied in the context of library APIs, "not thread safe" is simply shorthand for saying that, if you are likely to access instances of the class through multiple concurrent [threads][threading], you should provide your own thread synchronization mechanisms or, in the case of collections, look at the possibility of using a concurrent version of the class.
In the absence of these precautions, [race conditions][so-race-conditions] are likely to occur.
If your code base does not use multiple threads, and is not likely to, then this warning is of little concern.

[system-random]: https://docs.microsoft.com/en-us/dotnet/api/system.random
[random-thread-safety]: https://docs.microsoft.com/en-us/dotnet/api/system.random
Expand Down
3 changes: 2 additions & 1 deletion concepts/randomness/introduction.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Introduction

In C# randomness is achieved with the help of `System.Random`. Typically, you create an instance and then call one of its `Next()` or `NextDouble()` methods, possibly multiple times depending on the use-case.
In C# randomness is achieved with the help of `System.Random`.
Typically, you create an instance and then call one of its `Next()` or `NextDouble()` methods, possibly multiple times depending on the use-case.
6 changes: 3 additions & 3 deletions exercises/concept/roll-the-die/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

This [article][system-random] is an excellent introduction to the subject.

## 1. Enable a wizards and warriors player to roll a die.
## 1. Enable a _Wizards and Warriors_ player to roll a die

This [article][random-integers] explains how to generate integers.

## 2. Players need their strength. Provide a means to generate spell strength

This [article][random-reals] explains how to generate real numbers.
This [article][random-floating-point] explains how to generate floating-point numbers.

[system-random]: https://docs.microsoft.com/en-us/dotnet/api/system.random
[random-integers]: https://docs.microsoft.com/en-us/dotnet/api/system.random.next
[random-reals]: https://docs.microsoft.com/en-us/dotnet/api/system.random.nextdouble
[random-floating-point]: https://docs.microsoft.com/en-us/dotnet/api/system.random.nextdouble
12 changes: 8 additions & 4 deletions exercises/concept/roll-the-die/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Instructions

Continuing the theme of the wizards and warriors game, it is time to add an all purpose die rolling method. This will be the traditional 18 sided die with numbers 1 to 18. Players also generate a spell strength.
In this exercise, you are playing a role-playing game named "Wizards and Warriors."
To play this game, you need an all-purpose die rolling method to determine various outcomes.
This will be the traditional 18-sided die with numbers 1 to 18.
Players will also need to generate a random spell strength.

## 1. Enable a _wizards and warriors_ player to roll a die.
## 1. Enable a _Wizards and Warriors_ player to roll a die

Implement a `RollDie()` method on the `Player` class.

Expand All @@ -14,9 +17,10 @@ player.RollDie();

## 2. Players need their strength. Provide a means to generate spell strength

Implement a `GenerateSpellStrength()` method on the player class. The spell strength is between 0.0 and up to but not including 100.0.
Implement a `GenerateSpellStrength()` method on the player class.
The spell strength is between 0.0 and up to but not including 100.0.

Note that spell strength must be a real number (not an integer) to reduce the possibility of a tie when the strengths of two adversaries are compared.
Note that spell strength must be a floating-point number (not an integer) to reduce the possibility of a tie when the strengths of two adversaries are compared.

```csharp
var player = new Player();
Expand Down
3 changes: 2 additions & 1 deletion exercises/concept/roll-the-die/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

## Randomness

In C# randomness is achieved with the help of `System.Random`. Typically, you create an instance and then call one of its `Next()` or `NextDouble()` methods, possibly multiple times depending on the use-case.
In C# randomness is achieved with the help of `System.Random`.
Typically, you create an instance and then call one of its `Next()` or `NextDouble()` methods, possibly multiple times depending on the use-case.
3 changes: 2 additions & 1 deletion exercises/concept/roll-the-die/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"ErikSchierboom",
"yzAlvin",
"aage",
"karanchadha10"
"karanchadha10",
"yrahcaz7"
],
"files": {
"solution": [
Expand Down
Loading