POV-Ray : Newsgroups : povray.general : trouble using rand inside of sine function : Re: trouble using rand inside of sine function Server Time
30 Jul 2024 14:18:21 EDT (-0400)
  Re: trouble using rand inside of sine function  
From: Tor Olav Kristensen
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

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