Grasshopper

algorithmic modeling for Rhino

hi David,

I'm trying to write a component that will generate random spheres within a box. But the random method seems to only generate random number within certain number range so I end up with spheres that only cluster as certain region within my box. Am I not using the random method properly? The following is my code. Thanks!



Dim locPts As New List(Of Point3d)

    Dim sps As New List(Of Sphere)

    Dim bbox As BoundingBox
    bbox = box01.BoundingBox

    Dim rad As Double = 2


    Dim rndx As Random
    Dim rndy As Random
    Dim rndz As Random


    For i As Integer = 0 To fc Step 1

      rndx = New Random(i * 1)
      rndy = New Random(i * 2)
      rndz = New random(i * 5)


      Dim locPt As New Point3d(rndx.Next(bbox.Min.X + rad, bbox.Max.X - rad), rndy.Next(bbox.Min.Y + rad, bbox.Max.Y - rad), rndz.Next(bbox.Min.Z + rad, bbox.Max.Z - rad))



      Dim sp01 As New Sphere(locPt, rad)

      sps.Add(sp01)

    Next




    A = sps

Views: 440

Replies to This Discussion

Your using the Next method, which is fine, but only returns integers not doubles. I'm not sure if this really explains the issue your having, but this is the only thing that I see wrong. Here's how I'd rewrite the point generation...

Dim locPt as new Point3d ((rndx.NextDouble() * (bbox.Max.X - bbox.Min.X - 4))+ bbox.Min.X + 2), _
(rndy.NextDouble() * (bbox.Max.Y - bbox.Min.Y - 4))+ bbox.Min.Y + 2), _
(rndz.NextDouble() * (bbox.Max.Z - bbox.Min.Z - 4))+ bbox.Min.Z + 2))
I think you should only define Random once:

Dim rnd As New Random(4) 'Use a fixed seed for consistent results
For i As Integer = 0 To fc
  Dim locPt As New Point3d(rnd.Next....rnd.Next... etc. etc.)
Next


--
David Rutten
david@mcneel.com
Poprad, Slovakia
Thanks Damien and David,

It works quiet nicely now!!

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