Skip to content

[API Proposal]: Random NextDoubles(Span<double>) #129072

@ladeak

Description

@ladeak

Background and motivation

Fills the elements of a specified span of doubles with random numbers. Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.

This is a cross of the existing methods: NextBytes(Span<byte>) and NextDouble().

Can be useful when sampling multiple values from continuous distributions.

API Proposal

namespace System;

public partial class Random
{
+   public virtual void NextDoubles(Span<double> buffer);
}

API Usage

Span<double> buffer = stackalloc double[4];
Random.Shared.NextDoubles(buffer);
Console.WriteLine(buffer[0]);

Alternative Designs

Ideally, I'd like to load the random values into a Vector<T> for SIMD operation, but I am not aware if such SIMD operation is available on any architecture.

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.NumericsuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions