Git Product home page Git Product logo

Comments (2)

Gregor959 avatar Gregor959 commented on July 17, 2024

if somebody likes to randomly shuffle a Matrix, I have added a method to that in my latest commit.
ShuffleColumns() and ShuffleRows(). It also returns the Permuation so another matrix can be shuffled in the same order if it has the same size in the corresponding dimenesion.

Have also created a RandomPermuation method in Sorting as part of an extension class called SortingExtensions.
Perhaps the person requesting this ,can borrow that for now.
This can be used similary to how I used it in Matlab before.

from mathnet-numerics.

Gregor959 avatar Gregor959 commented on July 17, 2024

The code below generates random Permutations. Not sure what the original post is requiring.

''''
static readonly System.Random random = new System.Random(555);

    /// <summary>
    /// Creates a Random Permutation of lenght len (indexes 0 to len -1). Internally uses System.Random with the same seed.
    /// using RandomPermutation(int len, AbstractRandomNumberGenerator rnd) can give better randomness.
    /// </summary>
    /// <param name="len">the length of the Permuation</param>
    /// <returns></returns>
    public static Permutation RandomPermutation(int len)
    {

        int[] keys = new int[len];
        int[] items = new int[len];

        for (int i = 0; i < keys.Length; i++)
        {
            items[i] = i;
            lock (SortingExtensions.random)//System Random is not thread save!
            {
                keys[i] = SortingExtensions.random.Next();
            }
        }
        Sorting.Sort(keys, items);
        //although not neccessary here, the inverse of a Permutation corrseponds to a given sort order, if that Permuation was to be used to sort a matrix in the same order as the List.
        return new Permutation(items).Inverse();
    }

    /// <summary>
    /// Creates a Random Permutation of lenght len (indexes 0 to len -1) using a supplied random Number Generator
    /// </summary>
    /// <param name="len">the lenght of the permuation</param>
    /// <param name="rnd">The Random number Generator</param>
    /// <returns>The requested Permutation</returns>
    public static Permutation RandomPermutation(int len, AbstractRandomNumberGenerator rnd)
    {

        int[] keys = new int[len];
        int[] items = new int[len];

        for (int i = 0; i < keys.Length; i++)
        {
            items[i] = i;
            keys[i] = rnd.Next();
        }
        Sorting.Sort(keys, items);

        // The inverse of a Permutation corrseponds to a given sort order, 
        // if that Permuation were to be used to sort a matrix in the same order as the random List above.
        // Although it is neccessary here , since we are generating a random Permuation,
        // for consistancy the Permuation is also inversed. 
        return new Permutation(items).Inverse();
    }
}

''''

from mathnet-numerics.

Related Issues (20)

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.