Linear congruential generator: Difference between revisions
Content added Content deleted
(→{{header|rust}}: add rust) |
(→{{header|C sharp|C#}}: Added an other C# version (shorter)) |
||
Line 859: | Line 859: | ||
794471793 |
794471793 |
||
551188310 |
551188310 |
||
</pre> |
|||
==={{header|Shorter}}=== |
|||
<lang Csharp>using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using static System.Console; |
|||
namespace LinearCongruentialGenerator |
|||
{ |
|||
static class LinearCongruentialGenerator |
|||
{ |
|||
static int _seed = (int)DateTime.Now.Ticks; // from bad random gens might as well have bad seed! |
|||
static int _bsdCurrent = _seed; |
|||
static int _msvcrtCurrent = _seed; |
|||
static int Next(int seed, int a, int b) => (a * seed + b) & int.MaxValue; |
|||
static int BsdRand() => _bsdCurrent = Next(_bsdCurrent, 1103515245, 12345); |
|||
static int MscvrtRand() => _msvcrtCurrent = Next (_msvcrtCurrent << 16,214013,2531011) >> 16; |
|||
static void PrintRandom(int count, bool isBsd) |
|||
{ |
|||
var name = isBsd ? "BSD" : "MS"; |
|||
WriteLine($"{name} next {count} Random"); |
|||
var gen = isBsd ? (Func<int>)(BsdRand) : MscvrtRand; |
|||
foreach (var r in Enumerable.Repeat(gen, 10)) |
|||
WriteLine(r.Invoke()); |
|||
} |
|||
static void Main(string[] args) |
|||
{ |
|||
PrintRandom(10, true); |
|||
PrintRandom(10, false); |
|||
Read(); |
|||
} |
|||
} |
|||
}</lang> |
|||
Produces: |
|||
<pre>BSD next 10 Random |
|||
1587930915 |
|||
19022880 |
|||
1025044953 |
|||
1143293854 |
|||
1642451583 |
|||
1110934092 |
|||
773706389 |
|||
1830436778 |
|||
1527715739 |
|||
2072016696 |
|||
MS next 10 Random |
|||
24368 |
|||
8854 |
|||
28772 |
|||
16122 |
|||
11064 |
|||
24190 |
|||
23724 |
|||
6690 |
|||
14784 |
|||
21222 |
|||
</pre> |
</pre> |
||