POV-Ray : Newsgroups : povray.newusers : Random Number Generation Server Time
5 Nov 2024 13:24:34 EST (-0500)
  Random Number Generation (Message 1 to 5 of 5)  
From: Reuben Pearse
Subject: Random Number Generation
Date: 26 Feb 2001 11:29:52
Message: <3a9a8480@news.povray.org>
Hi all,

I have created a macro to create an object. I now want to randomly place the
object in my scene (within specified boundaries!!)
How do I get POVRay to generate random numbers?

Thanks,

Reuben


Post a reply to this message

From: Josh English
Subject: Re: Random Number Generation
Date: 26 Feb 2001 11:50:35
Message: <3A9A89B3.F7059895@spiritone.com>
The simplest way to do this is:
#declare lower_bound = something;
#declare upper_bound = somethingelse;

#declare MySeed = seed(2531); // or some other number, big and prime is nice

object { whatever
              translate lower_bound + rand(MySeed)*(upper_bound - lower_bound) }

// end
something and something else can be vectors, but they will distribute within a
straight line, so you will have to do this in triplicate for three different
vectors to fill space.


Reuben Pearse wrote:

> Hi all,
>
> I have created a macro to create an object. I now want to randomly place the
> object in my scene (within specified boundaries!!)
> How do I get POVRay to generate random numbers?
>
> Thanks,
>
> Reuben

--
Josh English -- Lexiphanic Lethomaniac
eng### [at] spiritonecom
The POV-Ray Cyclopedia http://www.spiritone.com/~english/cyclopedia/
"He who hebetates is last."


Post a reply to this message

From: Margus Ramst
Subject: Re: Random Number Generation
Date: 26 Feb 2001 13:06:13
Message: <3A9A9BF4.EC6B05B4@peak.edu.ee>
Reuben Pearse wrote:
> 
> I have created a macro to create an object. I now want to randomly place the
> object in my scene (within specified boundaries!!)
> How do I get POVRay to generate random numbers?
> 

First you have to declare a random number seed, like this:

#declare RandSeed = seed(N); //N is a random integer of your choice

The you can call the rand() function, with the name of the seed identifier as
the parameter, for examle like this:

#declare RandomValue=rand(RandSeed);

Each time you call rand(RandSeed) a different (pseudo)random value is returned
from the stream initialized by RandSeed.
For creating random numbers of a given mean value and maximum deviation, you can
try these macros (the first returns a float, the second depends on the first and
returns a 3D vector):

//Create random number of given mean and maximum deviation
//M - mean value
//D - maximum deviation
//Seed - (declared) random number seed identifier
#macro rand_ext(M,D,Seed)
    (M+(rand(Seed)-.5)*2*D)
#end

//Give a random vector of given mean and max deviation
//M - mean (vector or float)
//D - max deviation (vector or float)
//Seed - (declared) random number seed identifier
#macro v_rand_ext(M,D,Seed)
    #local MV=M+<0,0,0>;
    #local DV=D+<0,0,0>;
    (<rand_ext(MV.x,DV.x,Seed),
    rand_ext(MV.y,DV.y,Seed),
    rand_ext(MV.z,DV.z,Seed)>)
#end

-- 
Margus Ramst

Personal e-mail: mar### [at] peakeduee
TAG (Team Assistance Group) e-mail: mar### [at] tagpovrayorg
Home page http://www.hot.ee/margusrt


Post a reply to this message

From: Chris Huff
Subject: Re: Random Number Generation
Date: 26 Feb 2001 16:22:58
Message: <chrishuff-411956.16214626022001@news.povray.org>
In article <3A9A89B3.F7059895@spiritone.com>, Josh English 
<eng### [at] spiritonecom> wrote:

> something and something else can be vectors, but they will distribute 
> within a straight line, so you will have to do this in triplicate for 
> three different vectors to fill space.

Triplicate for three different vectors? Not quite, you just need three 
calls to rand()...try these macros:

// "signed rand", ranges from -1 to 1
#macro SRand(RS) (rand(RS)*2 - 1) #end

// "ranged rand"
#macro RRand(Mn, Mx, RS) (Mn + rand(RS)*(Mx-Mn)) #end

// random point in cube from Mn to Mx
#macro RandPt(Mn, Mx, RS)
    (Mn + < rand(RS), rand(RS), rand(RS)>*(Mx-Mn))
#end

-- 
Christopher James Huff
Personal: chr### [at] maccom, http://homepage.mac.com/chrishuff/
TAG: chr### [at] tagpovrayorg, http://tag.povray.org/

<><


Post a reply to this message

From: Josh English
Subject: Re: Random Number Generation
Date: 26 Feb 2001 16:33:52
Message: <3A9ACC15.43EEAC20@spiritone.com>
Chris Huff wrote:

> In article <3A9A89B3.F7059895@spiritone.com>, Josh English
> <eng### [at] spiritonecom> wrote:
>
> > something and something else can be vectors, but they will distribute
> > within a straight line, so you will have to do this in triplicate for
> > three different vectors to fill space.
>
> Triplicate for three different vectors? Not quite, you just need three
> calls to rand()...try these macros:
>
> // "signed rand", ranges from -1 to 1
> #macro SRand(RS) (rand(RS)*2 - 1) #end
>
> // "ranged rand"
> #macro RRand(Mn, Mx, RS) (Mn + rand(RS)*(Mx-Mn)) #end
>
> // random point in cube from Mn to Mx
> #macro RandPt(Mn, Mx, RS)
>     (Mn + < rand(RS), rand(RS), rand(RS)>*(Mx-Mn))
> #end

I feel so stupid for missing that. No wonder I gave up programming


--
Josh English -- Lexiphanic Lethomaniac
eng### [at] spiritonecom
The POV-Ray Cyclopedia http://www.spiritone.com/~english/cyclopedia/
"He who hebetates is last."


Post a reply to this message

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