POV-Ray : Newsgroups : povray.general : other random function Server Time
28 Nov 2024 02:55:46 EST (-0500)
  other random function (Message 1 to 3 of 3)  
From: stevenvh
Subject: other random function
Date: 6 Aug 2007 13:50:00
Message: <web.46b75deb83bdbb18e99dba500@news.povray.org>
rand() returns a value in [0..1[, which is as general as it gets, but I
often need a random value in [A-B..A+B].
Of course I could write

    #declare myvar = A + B * ( 2 * rand(rnd) - 1 );

but I would love to have a function, say rand2(), which I could use as e.g.

    #declare myvar = rand2(rnd, A, B);

or even

    sphere { < rand2(rnd, A, B), rand2(rnd, A, B), rand2(rnd, A, B) >, 100 }

Is this possible? I gather that I can't use rand() in user-defined
functions.
TIA

Steven


Post a reply to this message

From: Mike Williams
Subject: Re: other random function
Date: 6 Aug 2007 14:45:52
Message: <1Ru+7SAyw2tGFwsS@econym.demon.co.uk>
Wasn't it stevenvh who wrote:
>rand() returns a value in [0..1[, which is as general as it gets, but I
>often need a random value in [A-B..A+B].
>Of course I could write
>
>    #declare myvar = A + B * ( 2 * rand(rnd) - 1 );
>
>but I would love to have a function, say rand2(), which I could use as e.g.
>
>    #declare myvar = rand2(rnd, A, B);
>
>or even
>
>    sphere { < rand2(rnd, A, B), rand2(rnd, A, B), rand2(rnd, A, B) >, 100 }
>
>Is this possible? I gather that I can't use rand() in user-defined
>functions.

You can't use rand in functions, but you can use it in a macro. The
"rand.inc" include file contains this:

//random number in specified range [Min, Max]
#macro RRand(Min, Max, RS) (rand(RS)*(Max-Min) + Min) #end

You can either use that like  RRand(A-B,A+B,rs)  or write

//random number in specified range [A-B,A+B]
#macro ABRand(A, B, RS) (rand(RS)*2*B + A-B) #end


-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: stevenvh
Subject: Re: other random function
Date: 7 Aug 2007 04:25:00
Message: <web.46b82ba6e249db9e99dba500@news.povray.org>
Thanks Mike.

Ok, that was easy. :-)
Looks like I still have to get used to POVray's macros and functions.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.