Sign in to follow this  
treeway

Gradient Weighted random number

Recommended Posts

Hi

I have two points (A,B) and I want to create a set of random points between the two, such that most points are close to A and the number of points decreases linearly towards B. Does anyone know a simple way to solve this?

Thanks

Share this post


Link to post
Share on other sites
Random number generators generating values between zero and one with an uniform distribution are common. You can take the squared output of such a generator, and then use to to scale the vector (B-A) and add it to A.

In pseudocode:
[code]
uniformRand = rand();

outputPoint = A + uniformRand*uniformRand * (B-A);
[/code]

Share this post


Link to post
Share on other sites
[quote name='treeway' timestamp='1317807097' post='4869321']
I have two points (A,B) and I want to create a set of random points between the two, such that most points are close to A and the number of points decreases linearly towards B. Does anyone know a simple way to solve this?
[/quote]
It sounds like you need to generate random numbers on a geometric or exponential distribution. If you're using C++ and your development environment supports the current ISO standard, you can just use [font="Courier New"]std::geometric_distribution[/font] or [font="Courier New"]std::exponential_distribution[/font] from [font="Courier New"]<random>[/font]. If you're not in that situation, you can try to write them yourself, it's not that difficult.

Share this post


Link to post
Share on other sites
Thanks for the reply guys Rene Z's response worked perfectly for my situation (I'm not using C++), but I will look into std::geometric_distribution and std::exponential_distribution just out of interest, so thanks for that Bregma.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this