Linear congruential generator: Difference between revisions

m
→‎{{header|C sharp|C#}}: Removed unnecessary headers, re-ordered solutions
m (→‎{{header|Shorter}}: Minor code change does not change output)
m (→‎{{header|C sharp|C#}}: Removed unnecessary headers, re-ordered solutions)
Line 766:
 
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|6+}}
==={{header|Free Cell Deal}}===
<!-- By Martin Freedman, 17/01/2018 -->
<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, count))
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>
From a Free Cell Deal solution
<lang Csharp>
using System;
Line 860 ⟶ 922:
794471793
551188310
</pre>
==={{header|Shorter}}===
{{works with|C sharp|C#|6+}}
<!-- By Martin Freedman, 17/01/2018 -->
<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, count))
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>