Grasshopper

algorithmic modeling for Rhino

pick a number of non consecutive items randomly

Simple question. I have a list of numbers and I want to get randomly several of them (lets say two or three numbers), but they cannot be consecutive, their indexes must be separated by a distance of three or four units...

Views: 699

Replies to This Discussion

This only works if your initial list is long enough to allow you to pick N items with at least k items in between. That is an indication that the algorithm which picks these items has to have conditional statements, which make it a more complex type of algorithm.

A simpler solution (which doesn't enforce the strict minimum-distance constraint) would be to simply pick well spaced values from the list. You can use Populate Geometry for this as a geometric shortcut:

The logic is thus:

  1. Make a Line segments that has the same length as your list.
  2. Populate the line with well-spaced points using Populate Geometry.
  3. Extract the X coordinate of each point.
  4. Sort these x-coordinates if you want to pick items in ascending order, or remove the Sort component if you want to get items that are as far away from their neighbours as possible.
  5. Use the (sorted) x-coordinates as list indices to pick items out of the original list.

Clever and relatively simple, but not exactly what you asked for.

Another approach would be to generate a large amount of possible pickings, then remove the ones that don't result in a valid pattern and use one of the left-over patterns to finally pick your data:

The logic here is:

  1. Generate a large number of possible picking patterns.
  2. Sort them all from lowest number to largest number.
  3. For each pattern, figure out the distance between the nearest two consecutive items (i.e. the smallest gap size)
  4. Remove all patterns with a smallest gap size that is less than your minimum allowed gap size.
  5. Pick the first of all remaining patterns.
  6. Apply it to your original list.

--

David Rutten

david@mcneel.com

Tirol, Austria

Attachments:

Thank you very much David. I´ll play around with it.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service