Grasshopper

algorithmic modeling for Rhino

maybe someone can propose a better way but I need a list of random nonconsecutive integers. My plan was to look at the previous number in the list and exclude it from the possibilities. Is there a way to make a random choice amongst options rather than giving the random function a range? Or can you exclude a number from a  range?

thanks!

Views: 3263

Replies to This Discussion

Generate a large list of random integers. Then shift the list. Use an equality component to see if a number is identical to the next number. This will give you a list of booleans. Use these booleans to cull the original list. Now, extract a subset from this list.


--
David Rutten
david@mcneel.com
Poprad, Slovakia
Attachments:
thanks David!
Oh wait, this is the VB/C# forum, were you looking for a programmatic solution?

--
David Rutten
david@mcneel.com
Poprad, Slovakia
i was, but yours worked just as well. Im not that picky
I'd approach that slightly differently, as it's easier to add conditionals inside a Loop in textual programming.

Dim rnd As New Random()
Dim sequence As New List(Of Integer)(DesiredListLength)

Do
If (sequence.Count >= DesiredListLength) Then Exit Do
If (sequence.Count = 0) Then
sequence.Add(rnd.Next(min, max))
Else
Dim n_val As Integer = rnd.Next(min, max)
If (n_val <> sequence(sequence.Count-1)) Then
sequence.Add(n_val)
End If
End If
Loop

A = sequence

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Just curious, but the input values for the above would be

DesiredListLength
min
max

Did I pass the quiz?

If DesiredListLength = 10 then sequence becomes an empty list with a fixed length of 10? This I might be wrong about.
Taz,

right on the first assumption, incorrect on the second. When you construct a new List(Of WhatEver), you can supply an optional initial capacity.

By default a new List can contain 4 items. So if you create a new List(Of WhatEver) and you use the default constructor, you get a list with count=0 and capacity=4. When you try and add a fifth item, the list will realise there's no room left and it will double its internal memory to 8 slots. Now, when you try to add a ninth item, it will double again to 16 and so on and so forth. This behaviour has two drawbacks:

1) Growing the internal memory allocation takes some processor cycles.
2) It's possible the list grows unnecessarily big.

For example, imagine we're making a list of 10,000 items. Normally, the List would have to resize itself 12 times (4, 8, 16, 32, 64,..., 8192, 16384) and it finally ends up with over 6000 slots it doesn't need.

If however we constructed the list right away with a capacity for 10,000 items, i.e. New List(Of WhatEver)(10000), not a single resize operation is needed and we're not wasting any memory.

The capacity is always a hint, it is not possible to make a List(Of T) have a fixed size. The capacity will be increased automatically by the List class, which is why it is such a joy to use it.

If you don't know how many items you are going to store in a list you can always choose to call TrimExcess when you're done, which at least insures you're not wasting memory. This is only important though on lists that have a relatively long life-span.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
interesting, and useful. thanks David
I didn't realize what a loaded question that was...

Thanks for the explanation!

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