Skip to content

[API Proposal]: Change accessibility of System.Random's implementation #88138

@Xyncgas

Description

@Xyncgas

Background and motivation

Perhaps, while people can choose to use new System.Random() or System.Random.Shared, let it be possible to choose which implementation we are going to use, for example let people do new System.Random.Xorshiro()

Currently the actual name for the Xor shift rotation implementation in System.Random is XoshiroImpl and it's an internal class

I personally get something out of this if I can use specifically the flavor I'd like for my rng, because it's now possible to System.Random.Xorshiro().Seed(State), Initializing it with a state that was maybe extracted from State property

API Proposal

public sealed class XorShiro: ImplBase
{
...
public void Seed(){}
public static Random Seed(State state){}
public State State (){}
}

API Usage

var automaticAlgorithm = System.Random.Shared;

var manualAlgorithm = System.Random.XorShiro.Seed( :Span / number / state);
var state = manualAlgorithm.State;

for (var i = 0; i < 3; i++) manualAlgorithm.Next(); //1,1,1

var restore = System.Random.XorShiro.Seed(state);

for (var i = 0; i < 3; i++) restore.Next(); //1,1,1

Alternative Designs

Have people implement their own rng

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Runtimeneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions