POV-Ray : Newsgroups : povray.general : trouble using rand inside of sine function Server Time
2 Nov 2024 14:42:45 EDT (-0400)
  trouble using rand inside of sine function (Message 1 to 7 of 7)  
From: Mike
Subject: trouble using rand inside of sine function
Date: 4 Dec 2008 13:55:01
Message: <web.4938266b4d71081ac9b9f9f00@news.povray.org>
Hi.
I am trying to create a random phase within a sine function, but keep getting an
error that a 'function identifier' is expected and I do not understand why.

I am doing:
#declare R1=seed(1153);
#declare xspectrum =function{ sin  ( x+  rand(R1)   )    }

but it does not work. I do realize that for this case I could use:
#declare randomnum=rand(R1);
first and use that in my sine function, because this works, but I really need to
have the random number computed within the sine function. This is just a
simplified case, what I will really do is:

#declare xspectrum = function { sum(Kx,1,10,   .025/2*( sin(Kx*6.2832*x +
2*3.14159*rand(R1)   ) ) )  }

and here I definitely need the random number evaluated in the function.

Any ideas,
Mike


Post a reply to this message

From: Chris B
Subject: Re: trouble using rand inside of sine function
Date: 4 Dec 2008 17:39:04
Message: <49385c08@news.povray.org>
"Mike" <win### [at] hotmailcom> wrote in message 
news:web.4938266b4d71081ac9b9f9f00@news.povray.org...
> Hi.
> I am trying to create a random phase within a sine function, but keep 
> getting an
> error that a 'function identifier' is expected and I do not understand 
> why.
>
> I am doing:
> #declare R1=seed(1153);
> #declare xspectrum =function{ sin  ( x+  rand(R1)   )    }
>

Hi Mike,

The help says it's not supported.

It's not one of the FLOAT_FUNCTIONS listed in the 'user defined functions' 
section in the help.
Indeed the help specifies that only float functions that apply to float 
values may be used, which excludes rand because it takes an Integer value 
pointing to the predefined seed stream. Below the list of supported 
functions in my copy of the help 'rand' is specifically used as an example 
of functions that are not supported.

I think the reason for the constraint could be associated with the fact that 
functions are able to be interpreted at render-time (rather than just during 
the parse sequence). Someone may be able to give a more comprehensive 
description (I'm at the limit of my understanding here).

Hate to be the bearer of bad news :-)

Regards,
Chris B.


Post a reply to this message

From: clipka
Subject: Re: trouble using rand inside of sine function
Date: 4 Dec 2008 17:45:00
Message: <web.49385c92e3dfa9ccf5dae140@news.povray.org>
"Mike" <win### [at] hotmailcom> wrote:
> I am doing:
> #declare R1=seed(1153);
> #declare xspectrum =function{ sin  ( x+  rand(R1)   )    }
>
> but it does not work.

Unfortunately, only a subset of float expressions is supported in function
definitions. The rand() built-in function doesn't seem to be one of them.

You will have to find some other way.

How about a "noisy" pattern function, e.g.:

#declare myRand = function { pattern { bozo } }
#declare xspectrum = function { sin ( x + myRand(x,y,z) ) }

would this help you, maybe with a lot of turbulence added to the pattern?


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: trouble using rand inside of sine function
Date: 4 Dec 2008 18:34:46
Message: <49386916$1@news.povray.org>
Mike wrote:
> Hi.
> I am trying to create a random phase within a sine function, but keep getting an
> error that a 'function identifier' is expected and I do not understand why.
> 
> I am doing:
> #declare R1=seed(1153);
> #declare xspectrum =function{ sin  ( x+  rand(R1)   )    }
...
rand() can not be used inside functions.
See http://www.povray.org/documentation/view/3.6.1/231/

Do you really need it to be a function ?


If so you may want to have a look at f_noise3d(x, y, z) instead.
See http://www.povray.org/documentation/view/3.6.1/73/

To use it you will have to put

#include "functions.inc"

- somewhere in the beginning of your code.


If not, you can use a macro. Like this:

#version 3.6;

#macro X_Spectrum(X, SS)

   #local Sum =0;
   #local Kx = 1;
   #while (Kx <= 10)
     #local Sum = Sum + 0.025/2*sin(2*pi*(Kx*X + rand(SS)));
     #local Kx = Kx + 1;
   #end // while

   (Sum)

#end // macro X_Spectrum


You can use it e.g. like this:

#declare SUM = X_Spectrum(0.5, seed(65189));

#debug concat("\n\n", str(SUM, 0, -1), "\n\n")

-- 
Tor Olav
http://subcube.com


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: trouble using rand inside of sine function
Date: 4 Dec 2008 19:07:12
Message: <493870b0$1@news.povray.org>
Tor Olav Kristensen wrote:
> Mike wrote:
>> Hi.
>> I am trying to create a random phase within a sine function, but keep 
>> getting an
>> error that a 'function identifier' is expected and I do not understand 
>> why.
>>
>> I am doing:
>> #declare R1=seed(1153);
>> #declare xspectrum =function{ sin  ( x+  rand(R1)   )    }
> ...
> rand() can not be used inside functions.
> See http://www.povray.org/documentation/view/3.6.1/231/
> 
> Do you really need it to be a function ?
> 
> 
> If so you may want to have a look at f_noise3d(x, y, z) instead.
...

Or you can do something like this:

#version 3.6;

#macro BuildSpectrumFunction(Amplitude, Harmonics, SS)

   function(x) {
     Amplitude*
     (
       0
       #local K = 1;
       #while (K <= Harmonics)
         #local Phase = rand(SS);
         + sin(2*pi*(K*x + Phase))
         #local K = K + 1;
       #end // while
     )
   }

#end // macro BuildSpectrumFunction


#local A = 0.025/2;
#local H = 10;

#declare X_SpectrumFn1 = BuildSpectrumFunction(A, H, seed(65189))
#declare X_SpectrumFn2 = BuildSpectrumFunction(A, H, seed(3520))

#declare SUM1 = X_SpectrumFn1(0.50000);
#debug "\n"
#debug str(SUM1, 0, -1)
#declare SUM1 = X_SpectrumFn1(0.50001);
#debug "\n"
#debug str(SUM1, 0, -1)
#debug "\n\n"

#declare SUM2 = X_SpectrumFn2(0.50000);
#debug "\n"
#debug str(SUM2, 0, -1)
#declare SUM2 = X_SpectrumFn2(0.50001);
#debug "\n"
#debug str(SUM2, 0, -1)
#debug "\n\n"

-- 
Tor Olav
http://subcube.com


Post a reply to this message

From: Mike Williams
Subject: Re: trouble using rand inside of sine function
Date: 4 Dec 2008 20:02:03
Message: <bfBpQZDqyHOJFwch@econym.demon.co.uk>
Wasn't it Mike who wrote:
>Hi.
>I am trying to create a random phase within a sine function, but keep 
>getting an
>error that a 'function identifier' is expected and I do not understand why.

If you think about it, there's a pretty good reason why you can't use 
RAND inside a function. Imagine what happens when the code tries to 
solve such a function. The solver attempts to approach a solution by 
finding a series of points on each side that get nearer and nearer to 
the solution. If the function contains RAND, then the function is 
discontinuous everywhere, and that approach doesn't home in on a 
solution.

What you can do is write a macro instead of a function. You'll need to 
write your own loop to perform the sum() operation.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: Mike
Subject: Re: trouble using rand inside of sine function
Date: 5 Dec 2008 15:15:00
Message: <web.49398537e3dfa9ccc9b9f9f00@news.povray.org>
Thanks for all the help.
The macro seems to be working good.

-Mike Roberts


Post a reply to this message

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