Skip to content

[API Proposal]: Random.Next<T> #75431

@stephentoub

Description

@stephentoub

Background and motivation

Now that we have the generic math interfaces, we should be able to write a method for generating a random value that can work with any T where T implements the appropriate interface.

API Proposal

namespace System;

public partial class Random
{
    ...

    public T NextInteger<T>() where T : System.Numerics.IBinaryInteger<T>, System.Numerics.IMinMaxValue<T>;
    public T NextInteger<T>(T maxValue) where T : System.Numerics.IBinaryInteger<T>;
    public T NextInteger<T>(T minValue, T maxValue) where T : System.Numerics.IBinaryInteger<T>;

    public T NextFloat<T>() where T : IBinaryFloatingPointIeee754<T>
}

API Usage

Random r = new Random();
Int128 value = r.NextInteger<Int128>();

Alternative Designs

  • Naming: NextInteger vs Next, NextFloat vs ?

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Numericsin-prThere is an active PR which will close this issue when it is merged

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions