✅ Faster & thread safe random number generator?

Is there a good library for getting pseudo random numbers, faster than System.Random? I've done some looking but had a hard time coming up with much.
37 Replies
Angius
Angius5mo ago
Do you find the speed of System.Random a bottleneck?
MechWarrior99
MechWarrior99OP5mo ago
Yeah, not a massive one, but definitely is one.
Angius
Angius5mo ago
Are you using multiple instances of Random, or Random.Shared? Or some singleton you yourself made
MechWarrior99
MechWarrior99OP5mo ago
I have a large process which I create a new instance of Random for each time, since the process runs in a Task in a separate thread.
Angius
Angius5mo ago
Random.Shared is thread-safe, IIRC
ParaLogia
ParaLogia5mo ago
Random.Shared Property (System)
Provides a thread-safe Random instance that may be used concurrently from any thread.
MODiX
MODiX5mo ago
Angius
REPL Result: Success
using System;
using System.Threading.Tasks;

Parallel.For(0, 10, x =>
{
var value = Random.Shared.Next();
Console.WriteLine(value);
});
using System;
using System.Threading.Tasks;

Parallel.For(0, 10, x =>
{
var value = Random.Shared.Next();
Console.WriteLine(value);
});
Console Output
1562793860548352853
252507751
1548914677
1900456204
1478899365
11502557
1227585618
1038092387

1986991436
1562793860548352853
252507751
1548914677
1900456204
1478899365
11502557
1227585618
1038092387

1986991436
Compile: 356.154ms | Execution: 96.933ms | React with ❌ to remove this embed.
Angius
Angius5mo ago
Well this got one empty line lmao Could just be Modix though
MechWarrior99
MechWarrior99OP5mo ago
Ah good to know, but I actually reset the seed each time, which is why I create a new instance each time.
Angius
Angius5mo ago
You need deterministic random?
MechWarrior99
MechWarrior99OP5mo ago
Yeah
Angius
Angius5mo ago
Then I don't think you'll get much faster than System.Random Though you can always look for some nugers Maybe there's something that provides slightly worse randomness but is more performant I only know a less-performant way, RandomNumberGenerator, but this one's cryptographically secure
MechWarrior99
MechWarrior99OP5mo ago
Yeah I don't really mind if it is a bit worse randomness (I think at least haha)
ParaLogia
ParaLogia5mo ago
xkcd: Random Number
From An unknown user
RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.
MechWarrior99
MechWarrior99OP5mo ago
I remember seeing something about using xor-shift to get random numbers, but couldn't find a lot on it. 🤔
Angius
Angius5mo ago
https://en.wikipedia.org/wiki/Xorshift Seems simple to implement
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius5mo ago
Ah
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius5mo ago
Ooooh, I was wondering what's that long number lol
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
ParaLogia
ParaLogia5mo ago
lol
MODiX
MODiX5mo ago
TeBeCo
REPL Result: Success
using System;
using System.Threading.Tasks;

Parallel.For(0, 50, x =>
{
Console.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaa");
});
using System;
using System.Threading.Tasks;

Parallel.For(0, 50, x =>
{
Console.WriteLine("aaaaaaaaaaaaaaaaaaaaaaaa");
});
Console Output
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaa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aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaa
aaa
Compile: 380.625ms | Execution: 95.150ms | React with ❌ to remove this embed.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
MechWarrior99
MechWarrior99OP5mo ago
Oh thanks for the link. I didn't realize that is literally what it is called. Or that it seems pretty easy to implement. I might give that a go and see how it performs. Still a bit surprised there isn't more libraries for C# that deal with random numbers and randomness (just in general)
MODiX
MODiX5mo ago
Angius
REPL Result: Success
using System;
using System.Threading.Tasks;
using System.Collections.Concurrent;

ConcurrentBag<int> cb = [];
Parallel.For(0, 10, x =>
{
var val = Random.Shared.Next();
cb.Add(val);
});
Console.WriteLine(string.Join('\n', cb));
using System;
using System.Threading.Tasks;
using System.Collections.Concurrent;

ConcurrentBag<int> cb = [];
Parallel.For(0, 10, x =>
{
var val = Random.Shared.Next();
cb.Add(val);
});
Console.WriteLine(string.Join('\n', cb));
Console Output
1727679263
1768007445
1079044291
156279848
1635630282
1864601133
395743361
173183142
1376591690
536152903
1727679263
1768007445
1079044291
156279848
1635630282
1864601133
395743361
173183142
1376591690
536152903
Compile: 469.303ms | Execution: 109.025ms | React with ❌ to remove this embed.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius5mo ago
This should be thread-safe 99% of the time it's just not needed
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
MechWarrior99
MechWarrior99OP5mo ago
Yeah, guess that is true. So... not that surprising haha Huh interesting, thanks for the link
Angius
Angius5mo ago
Btw, just so we know what we're working with, what's the .NET version you're using?
MechWarrior99
MechWarrior99OP5mo ago
I'm using .NET 8.0
Angius
Angius5mo ago
Good, good
sibber
sibber5mo ago
xorshift is an option if you want to sacrifice even randomness for performance
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo ago
Stop asking about XorShift, just use System.Random and probably also Random.Shared.Next.... quote from the article bellow:
Despite the similar-sounding name, the “Xoroshiro/Xoshiro” algorithms approaches the problem differently from the preceding “Xorshift” family.
Note: Versions of the Xoshiro number generator are used in the System.Random class in the forthcoming .NET 6.x onwards
https://blogs.siliconorchid.com/post/coding-inspiration/randomness-in-dotnet
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server