Grasshopper

algorithmic modeling for Rhino

Hi,

Is it possible to use function  System.Random.Range(i, list.length) ?

When I type System.Random I cannot access Range.

I would like just to shuffle items in a list similar to this:

  1. for (int i = 0; i /span> alpha.Count; i++) {
  2. string temp = alpha[i];
  3. int randomIndex = Random.Range(i, alpha.Count);
  4. alpha[i] = alpha[randomIndex];
  5. alpha[randomIndex] = temp;
  6. }

Thanks,

Petras

Views: 1813

Replies to This Discussion

Where did you find a method called Range on the Random class? I can't see it on the MSDN documentation.

It looks like I was looking at a wrong place.

Could you help me to shuffle objects in a list?

Thanks,

Petras

I usually create an array of doubles and then sort that array while also sorting the other collection. But you'd have to convert the list to an array first.

Top of my head:

public void ShuffleList<T>(List<T> list, int seed)

{

  T[] array0 = list.ToArray();

  double[] array1 = new double[list.Count];

  Random random = new Random(seed);

  for (int i = 0; i < array1.Length; i++)

    array[i] = random.NextDouble();

  System.Array.Sort(ref array1, ref array0);

  list.Clear();

  list.AddRange(array0);

}

Make chaos, take chaos  ... whatever comes first.

he he

Attachments:

Dear David and Peter,

Thank you for the help for the chaos:)

Just one more  question. How does this line work?

data.OrderBy(x => rnd.Next()).ToList();

This is a LINQ (Google that) thingy (kinda a "SQL" (kinda) type of "language"):

https://msdn.microsoft.com/en-us/library/vstudio/bb534966%28v=vs.10...

For instance if you want to find things that (a) are objects, (b) of type slider, (c) where their nickname starts with ... er ... "LordOfDarkness" and (d) put them into a List you type the following:

BTW: LINQ may be cool but is slow.

BTW: Since you are after chaos ... what about adding chaos to chaos? See attached.

Attachments:

LINQ is rather a handy way to create "expressions" for handling collections (but slow). See this (a bit more complex, NOT related with chaos - just a LINQ demo, he he) portion of some code that does ... er ... something:

BTW: Get this all-time-classic LINQ stuff from The Master Of All C# things.

Attachments:

Thanks for the information. Is this procedure slower than David Rutten script?

Both Array.Sort and OrderBy will probably end up using quicksort (depending on the number of elements). If you really care about performance, use the Fisher–Yates shuffle (https://en.m.wikipedia.org/wiki/Knuth_shuffle) where you go through the list just once.
Doesn't make sense to care about the speed of linq if you're using a slow algoritm.

Make chaos, take chaos, skin a cat with 3 ways ... whatever comes first.

Attachments:

BTW: Here's some random points related stuff.

Use big (*) numbers and test them against the 3 ways (to skin that cat, that is).

(*): Like Godzilla: bigger is better.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service