Linear congruential generator: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 582:
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
{{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>
From a Free Cell Deal solution
<lang Csharp>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace FreeCellDeals
{
public class LCG
{
private int _state;
public bool Microsoft { get; set;}
public bool BSD
{
get
{
return !Microsoft;
}
set
{
Microsoft = !value;
}
}
 
public LCG(bool microsoft = true)
{
_state = (int)DateTime.Now.Ticks;
Microsoft = microsoft;
}
 
public LCG(int n, bool microsoft = true)
{
_state = n;
Microsoft = microsoft;
}
 
public int Next()
{
if (BSD)
{
return _state = (1103515245 * _state + 12345) & int.MaxValue;
}
return ((_state = 214013 * _state + 2531011) & int.MaxValue) >> 16;
}
 
public IEnumerable<int> Seq()
{
while (true)
{
yield return Next();
}
}
}
 
class Program
{
static void Main()
{
LCG ms = new LCG(0, true);
LCG bsd = new LCG(0,false);
Console.WriteLine("Microsoft");
ms.Seq().Take(10).ToList().ForEach(Console.WriteLine);
Console.WriteLine("\nBSD");
bsd.Seq().Take(10).ToList().ForEach(Console.WriteLine);
Console.ReadKey();
}
}
}
</lang>
Output:
<pre>Microsoft
38
7719
21238
2437
8855
11797
8365
32285
10450
30612
 
BSD
12345
1406932606
654583775
1449466924
229283573
1109335178
1051550459
1293799192
794471793
551188310
</pre>
 
=={{header|C++}}==
Line 770 ⟶ 929:
4 229283573 8855
5 1109335178 11797</pre>
 
=={{header|C sharp|C#}}==
{{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>
From a Free Cell Deal solution
<lang Csharp>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace FreeCellDeals
{
public class LCG
{
private int _state;
public bool Microsoft { get; set;}
public bool BSD
{
get
{
return !Microsoft;
}
set
{
Microsoft = !value;
}
}
 
public LCG(bool microsoft = true)
{
_state = (int)DateTime.Now.Ticks;
Microsoft = microsoft;
}
 
public LCG(int n, bool microsoft = true)
{
_state = n;
Microsoft = microsoft;
}
 
public int Next()
{
if (BSD)
{
return _state = (1103515245 * _state + 12345) & int.MaxValue;
}
return ((_state = 214013 * _state + 2531011) & int.MaxValue) >> 16;
}
 
public IEnumerable<int> Seq()
{
while (true)
{
yield return Next();
}
}
}
 
class Program
{
static void Main()
{
LCG ms = new LCG(0, true);
LCG bsd = new LCG(0,false);
Console.WriteLine("Microsoft");
ms.Seq().Take(10).ToList().ForEach(Console.WriteLine);
Console.WriteLine("\nBSD");
bsd.Seq().Take(10).ToList().ForEach(Console.WriteLine);
Console.ReadKey();
}
}
}
</lang>
Output:
<pre>Microsoft
38
7719
21238
2437
8855
11797
8365
32285
10450
30612
 
BSD
12345
1406932606
654583775
1449466924
229283573
1109335178
1051550459
1293799192
794471793
551188310
</pre>
 
=={{header|D}}==
Line 1,299:
794471793 10450
551188310 30612</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 04-11-2016
Line 2,055 ⟶ 2,056:
10450
30612</lang>
 
=={{header|Perl 6}}==
 
We'll define subroutines implementing the LCG algorithm for each version. We'll make them return a lazy list.
 
<lang perl6>constant modulus = 2**31;
sub bsd {
$^seed, ( 1103515245 * * + 12345 ) % modulus ... *
}
sub ms {
map * +> 16, (
$^seed, ( 214013 * * + 2531011 ) % modulus ... *
)
}
say 'BSD LCG first 10 values (first one is the seed):';
.say for bsd(0)[^10];
say "\nMS LCG first 10 values (first one is the seed):";
.say for ms(0)[^10];</lang>
 
<pre>BSD LCG first 10 values (first one is the seed):
0
12345
1406932606
654583775
1449466924
229283573
1109335178
1051550459
1293799192
794471793
 
MS LCG first 10 values (first one is the seed):
0
38
7719
21238
2437
8855
11797
8365
32285
10450</pre>
 
=={{header|Phix}}==
Line 2,415 ⟶ 2,372:
(define ms-rand (rand ms-update (λ (x) (quotient x (expt 2 16)))))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
We'll define subroutines implementing the LCG algorithm for each version. We'll make them return a lazy list.
 
<lang perl6>constant modulus = 2**31;
sub bsd {
$^seed, ( 1103515245 * * + 12345 ) % modulus ... *
}
sub ms {
map * +> 16, (
$^seed, ( 214013 * * + 2531011 ) % modulus ... *
)
}
say 'BSD LCG first 10 values (first one is the seed):';
.say for bsd(0)[^10];
say "\nMS LCG first 10 values (first one is the seed):";
.say for ms(0)[^10];</lang>
 
<pre>BSD LCG first 10 values (first one is the seed):
0
12345
1406932606
654583775
1449466924
229283573
1109335178
1051550459
1293799192
794471793
 
MS LCG first 10 values (first one is the seed):
0
38
7719
21238
2437
8855
11797
8365
32285
10450</pre>
 
=={{header|REXX}}==
Line 2,522 ⟶ 2,524:
p (1..5).map {lcg.rand}
# prints [41, 18467, 6334, 26500, 19169]</lang>
 
 
=={{header|Run BASIC}}==
Line 2,553 ⟶ 2,554:
9 794471793 10450
10 551188310 30612</pre>
 
 
=={{header|Rust}}==
Line 2,776:
MS Values: [38,7719,21238,2437,8855,11797,8365,32285,10450,30612]
</pre>
 
=={{header|Sidef}}==
{{trans|Ruby}}
Line 3,194 ⟶ 3,195:
794471793 10450
551188310 30612</pre>
 
=={{header|X86 Assembly}}==
 
10,327

edits