Grasshopper

algorithmic modeling for Rhino

Hi, I'm probably just confused about the syntax but i'm having a problem with the system.Random() class in grasshopper's c# script component. I'm trying to generate a vector with the x and y values somewhere between -1 and 1.

Here's my code:

System.Random rand = new System.Random();
vel = New On3dVector(rand.NextDouble(-1,1), rand.NextDouble(-1,1), 0);

Grasshopper gives me the following:
"Error: No overload for method 'NextDouble' takes '2' arguments"

It works with nothing in the parentheses: rand.NextDouble(); suggesting that the c# component does not support anything but the basic (clock seeded) option for System.random(). any ideas?

Views: 3980

Replies to This Discussion

Hi Chris,

the seed is in the constructor:
new System.Random();
     vs.
new System.Random(12);


Here an inheriting class that adds the overloads that you are looking for:
//New RangedRandom class definition that inherits from System.Random
class RangedRandom : System.Random
{
//clock-seeded constructor
public RangedRandom(): base(){}

//seeded constructor
public RangedRandom(int seed): base(seed){}

public double NextDouble(double max){
return NextDouble() * max;
}

public double NextDouble(double min, double max){
return (max - min) * NextDouble() + min;
}
}

Please find attached here also a scripting component with the setup.

- Giulio
_________________
giulio@mcneel.com
McNeel Europe, Barcelona
Attachments:
NextDouble does not allow you to specify the range of numbers, it only returns a random number between 0.0 and 1.0 (but never including 1.0). You can only specify the numeric range if you use System.Random to generate Integers.

--
David Rutten
david@mcneel.com
Poprad, Slovakia
Wow you guys are so great. thanks for all your help, both here and elsewhere if the forums where I've benefited from literally hundreds of your answers to other peoples' questions! (not to mention scripts and definitions you've written)

Just out of curiosity: I noticed that the System.random.NextDouble() class listed in microsoft's support page does indeed support 2 arguments (http://msdn.microsoft.com/en-us/library/system.random.aspx). But in GH, we use the RangedRandom class. why?

Are grasshopper's .Net classes generally different from those found in Microsoft's descriptions, and if so where can I expect differences to occur, and where can I expect them to be the same? Are there any references (lke the SDK) that list System classes with examples that are GH specific? ie: Giulio, where did you learn about rangedRandom.

I'm just beginning to figure this out so please excuse if I'm not making any sense!

-Chris
I can't find any documentation that suggests there is an overload of Random.NextDouble that takes any arguments. Only Random.Next has overloads that allow you to specify ranges, but Random.Next only returns integers.

When your code runs inside Grasshopper then you have access to both Grasshopper Types (which I wrote), Rhino SDK types (which the guys in Seattle wrote) and DotNET Framework Types (which the guys and gals in Redmond wrote).

--
David Rutten
david@mcneel.com
Poprad, Slovakia
RangedRandom (capitalization matters) is just written by me - you can see the implementation above. It maps the random interval from [0;1) to other intervals that you can choose.

- Giulio
_________________
giulio@mcneel.com
McNeel Europe, Barcelona
David and Giulio
Thanks that's very helpful. I think I'm starting to get it. Keep up the great work!

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