Code Monkey home page Code Monkey logo

randomnumbers's Introduction

RandomNumbers

C# Random class provides functionality to generate random numbers in C#. The Random class can also generate other data types including strings. In this code example, learn how to create a random number in C#.

By default, the .NET framework generates random numbers based on the current system time. Numbers generated using the Random class are not considered reliable for high-stake scenarios such as cryptography because the system clock has limited granularity. For example, two Random instances created withing couple of milliseconds yield the same sequence of values.

The Random class has a constructor that accepts the seed value. Seed is a a starting value for the pseudo-random number generation algorithm. If we use the same seed for different Random objects, they will generate the same series of random numbers. This functionality is useful when we want reproducibility.

Method Description
Next() Returns a positive random integer within the default range -2,147,483,648 to 2,147,483, 647.
Next(int) Returns a positive random integer that is less than the specified maximum value.
Next(int, int) Returns a positive random integer within the specified minimum and maximum range (includes min and excludes max).
NextDouble() Generates random floating-point number that is greater than or equal to 0.0 and less than 1.0.
NextByte() Fills the specified array with the random bytes.

Random permutations can be generated using Fisher-Yates shuffle algorithm. Time is taken proportionally to the toal number of items being shuffled and shuffles them in place. The algorithm swaps the element at each iteration at random among all remaining unvisted indices, including the element itself.

Fisher-Yates shuffle

private static Random rng = new Random();

public static void Shuffle<T>(this IList<T> list)
{
    int n = list.Count;
    while (n > 1)
    {
        n--;
        int k = rng.Next(n + 1);
        T value = list[k];
        list[k] = list[n];
        list[n] = value;
    }
}

A very similar algorithm to Fisher-Yates which produces a shuffled version of input is Sattolo's algorithm. Instead of picking an element at random to swap with, like in Fisher-Yates, it picks an element at random that is not the element being placed, i.e it does not allow an element to be swapped with itself. One side effect of this is that no element ends up where it originally started.

Sattolo's algorithm

private static readonly Random Rand = new Random();

void sattoloCycle<T>(IList<T> items) 
{
    for (var i = items.Count; i-- > 1;) 
    {
        int j = Rand.Next(i);
        var tmp = items[i];
        items[i] = items[j];
        items[j] = tmp;
    }
}

randomnumbers's People

Contributors

vgmda avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.