Anduin.SuperRandom 8.0.0

dotnet add package Anduin.SuperRandom --version 8.0.0
NuGet\Install-Package Anduin.SuperRandom -Version 8.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Anduin.SuperRandom" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Anduin.SuperRandom --version 8.0.0
#r "nuget: Anduin.SuperRandom, 8.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Anduin.SuperRandom as a Cake Addin
#addin nuget:?package=Anduin.SuperRandom&version=8.0.0

// Install Anduin.SuperRandom as a Cake Tool
#tool nuget:?package=Anduin.SuperRandom&version=8.0.0

SuperRandom

MIT licensed Pipeline stat Test Coverage NuGet version (Anduin.SuperRandom) ManHours

Non-repeating random numbers.

How to install

This project is built based on .NET 6.

dotnet add package Anduin.SuperRandom

Why do I need this

The traditional methods for obtaining n non-repeating random numbers are:

  • The random number is generated by the linear congruence method, and each random number is generated and compared in the database. If it already exists, the number is discarded.
  • Randomly generate a linear sequence, and then shuffle the sequence.

The time complexity of the above traditional method is O (n ^ 2). Especially in the case of larger data, for the first algorithm: after the generation of a large number of numbers already exists, the performance will become slower and slower. For the second method, it will take up very high memory space.

SuperRandom is based on the RSA algorithm, whose time complexity is O (n) and space complexity is O (1). It is only linearly related to the number of random numbers required, and does not need to be compared with any data set. So the performance is fast. Especially when it is over one million, it is obviously superior to any random number algorithm that depends on storage.

How to use

using Anduin.SuperRandom;

foreach (var rand in new Randomizer().GetRandomNumbers(1000000))
{
    Console.WriteLine("Got random number: " + rand);
}

Demo

For example, call this method to generate 15 non-repeat random numbers less than 15:

Got random number: 9
Got random number: 10
Got random number: 14
Got random number: 4
Got random number: 5
Got random number: 6
Got random number: 0
Got random number: 1
Got random number: 11
Got random number: 12
Got random number: 13
Got random number: 2
Got random number: 3
Got random number: 7
Got random number: 8
  • Generate unique invitation codes, and lottery codes
  • Generate non-incrementing non-duplicate primary key (for example, generate ID number)
  • Other scenes that require non-repeating random numbers

Note: Not recommended for use in lottery tickets, verification codes, etc. The random numbers generated by this algorithm are weak pseudo-random numbers, especially the randomness at the beginning is poor. It is possible to get the whole sequence through brute force calculation.

What it does

N is a positive integer.
Let a positive integer A be distributed in the set [2, (N-1)]
C = (A ^ D) mod N
And meet the following conditions:

D is prime
N is the product of two prime numbers (P, Q)
P! = Q
(D * E) mod ((P-1) * (Q-1)) = 1

∵ C = (A ^ D) mod N
∴ A = (C ^ E) mod N
∴ C and A are one-to-one mapping
∵ A is distributed in the set [2, (N-1)]
∴ A does not repeat, no omission
∴ C does not repeat, no omission
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 103 2/20/2024
7.0.2 96 2/2/2024
7.0.1 220 11/2/2023
7.0.0 130 9/26/2023
6.0.0 143 7/14/2023
1.0.0 485 5/7/2020