POV-Ray : Newsgroups : povray.newusers : arrays and random numbers Server Time
5 Sep 2024 18:23:09 EDT (-0400)
  arrays and random numbers (Message 1 to 7 of 7)  
From: CMcCabe
Subject: arrays and random numbers
Date: 8 Apr 2000 22:06:26
Message: <38EFE55D.ADB3B7E5@fcmail.com>
ok, after seeing the recent posts of Sphere and Superellipsoid world in
the text.scene-files and binaries.images i tried to write something that
i thought would produce a similiar effect
upon trying to render it i came up with a message saying 'cannot assign
uninitialized identifier'
this error was produced when it got to this line:
#declare CityArray[XPos][ZPos]=RNum;
the RNum is declared on this line:
#declare RNum = int ( rand ( seed(802) ) );
i'm sort of assuming that i have something wrong with the way i'm using
the random number generator, but i'm not entirely sure
basically i'm trying to assign a random value to that position of the
array

any help would be greatly appreciated
CMcCabe


Post a reply to this message

From: Ken
Subject: Re: arrays and random numbers
Date: 8 Apr 2000 23:52:05
Message: <38EFFE89.1B16A047@pacbell.net>
CMcCabe wrote:
> 
> ok, after seeing the recent posts of Sphere and Superellipsoid world in
> the text.scene-files and binaries.images i tried to write something that
> i thought would produce a similiar effect
> upon trying to render it i came up with a message saying 'cannot assign
> uninitialized identifier'
> this error was produced when it got to this line:
> #declare CityArray[XPos][ZPos]=RNum;
> the RNum is declared on this line:
> #declare RNum = int ( rand ( seed(802) ) );
> i'm sort of assuming that i have something wrong with the way i'm using
> the random number generator, but i'm not entirely sure
> basically i'm trying to assign a random value to that position of the
> array
> 
> any help would be greatly appreciated
> CMcCabe

You need to pre-declare the seed value. It cannot be used directly inside
the rand statement.

#declare R = seed(802);

#declare RNum = int ( rand ( R ) );

-- 
Ken Tyler -  1300+ Povray, Graphics, 3D Rendering, and Raytracing Links:
http://home.pacbell.net/tylereng/index.html http://www.povray.org/links/


Post a reply to this message

From: Chris Huff
Subject: Re: arrays and random numbers
Date: 9 Apr 2000 13:46:06
Message: <chrishuff_99-7AC712.12484009042000@news.povray.org>
In article <38EFE55D.ADB3B7E5@fcmail.com>, CMcCabe 
<dea### [at] fcmailcom> wrote:

> i'm sort of assuming that i have something wrong with the way i'm using
> the random number generator, but i'm not entirely sure
> basically i'm trying to assign a random value to that position of the
> array

As Ken explained, you need to pre-declare the seed:
#declare RS = seed(802);
#declare RNum = int(rand(RS));

However, if this is the code you are using, I think you will be running 
into another problem. The int() function operates like this(quoted from 
the manual):

"int(A)
 Integer part of A. Returns the truncated integer part of A. Rounds 
towards zero."

Since the rand() function returns float values between 0 and 1 
inclusively, you will get 0 most of the time, maybe all of the time. You 
need to at least include a multiplier in there:
#declare RNum = int(rand(RS)*Mult);
where Mult>1.

-- 
Christopher James Huff - Personal e-mail: chr### [at] yahoocom
TAG(Technical Assistance Group) e-mail: chr### [at] tagpovrayorg
Web page: http://chrishuff.dhs.org/


Post a reply to this message

From: CMcCabe
Subject: Re: arrays and random numbers
Date: 9 Apr 2000 20:01:11
Message: <38F1196B.30A6CB16@fcmail.com>
I realized this myself after looking at the code to the sphere and
superellipsoid scene again.  But thank you both very much for explaining that
random numbers can't be seeded and found at the same time.
Now I seem to be having another problem...
Is it possible to only initialize some positions of an array?  As in maybe
every array[X] where X is even?
Here's the code that I am trying to use:
#declare SNum = seed(8);
#declare RNum = int ( 10 * rand ( SNum ) );
#declare XPos = 0;
#declare ZPos = 0;
#declare CityArray = array [100][100]
#declare FCount = 0;

#declare Floor =
box { <XPos,0,ZPos>, <XPos+9,8,ZPos+9> pigment {Blue} }

#while (XPos < 100)
  #while (ZPos < 100 )
    #declare CityArray[XPos][ZPos]=RNum;
    #while (FCount <= CityArray[XPos][ZPos])
      object{Floor translate <0,FCount*8,0>}
      #declare FCount = FCount + 1;
    #end//while fcount
    #declare ZPos = ZPos + 10;
  #end//while zpos
  #declare XPos = XPos + 10;
#end//while xpos

When I run this I get a scene containing four floor objects, although I
thought that there would be many more floor objects than that.  So it seems
to me that the while loops are executing once and only once.  Any ideas as to
why would be greatly appreciated.

CMcCabe


Post a reply to this message

From: Robert Chaffe
Subject: Re: arrays and random numbers
Date: 9 Apr 2000 20:43:18
Message: <38f123a6@news.povray.org>
Perhaps resetting loop counters will do the trick?
Warning: untested!  New lines shown without ">" symbol ...

> [ snip ]
>
> #while (XPos < 100)
>   #while (ZPos < 100 )
>     #declare CityArray[XPos][ZPos]=RNum;
>     #while (FCount <= CityArray[XPos][ZPos])
>       object{Floor translate <0,FCount*8,0>}
>       #declare FCount = FCount + 1;
>     #end//while fcount
>     #declare ZPos = ZPos + 10;

       #declare FCount = 0;    // reset inner loop counter.

>   #end//while zpos
>   #declare XPos = XPos + 10;

     #declare ZPos = 0;    // reset middle loop counter.

> #end//while xpos

Does that help?   rc


Post a reply to this message

From: Chris Huff
Subject: Re: arrays and random numbers
Date: 10 Apr 2000 14:32:12
Message: <chrishuff_99-278016.13344710042000@news.povray.org>
In article <38F1196B.30A6CB16@fcmail.com>, CMcCabe 
<dea### [at] fcmailcom> wrote:

> When I run this I get a scene containing four floor objects, although 
> I thought that there would be many more floor objects than that.  So 
> it seems to me that the while loops are executing once and only once. 
>  Any ideas as to why would be greatly appreciated.

Well, someone else mentioned that you need to reset the loop counter 
variables. There are some serious problems with your code in addition to 
this.
The first two problems stem from the fact that you are initializing your 
loop counters before everything else. Since they are not reset within 
the inner loops, those loops complete on the first runthrough and are 
skipped after that. The best solution is to put the counter 
initialization immediately in front of the #while command.

The second problem is that you declare your Floor object to depend on 
the current position, but outside of the loop. Since XPos and ZPos are 
0, and you only translate in the y direction inside the loop, you will 
end up with a bunch of boxes occupying the same space. It will appear 
like one building. You need to declare your Floor object independant of 
position, or declare it inside the loop(in which case you might as well 
use it directly, no need to declare it).

Third, you assign a random number to a variable, and then use that 
variable a bunch of times within the loop. Since you only call rand() 
once, in the declaration, you will only get one number. All your 
buildings will be the same height. You need to get a new value from 
rand() each time you want a new random number.

And finally, unless you intend to use the array of data later in other 
code, you don't even need an array. Your code assigns a value to a 
position in the array, and then immediately uses that value. You can use 
the value directly, and save memory and parse time.

You seem to have a basic misunderstanding of how loops work. Only the 
portion between #while and it's matching #end is repeated, and the 
counter values only change in that portion. Counter variables are not 
reset after the loop.

This is what I think you want:

camera {
    location < 1, 5,-16>*10
    look_at < 0, 4, 0>
}
light_source {<-10, 20,-15>*1000 color White}
light_source {< 1, 5,-16>*10 color White*0.15}

#declare SNum = seed(8);
#declare Floor =
box {<0,0,0>, <9,8,9> pigment {Blue}}

#declare XPos=0;
#while(XPos<100)
    #declare ZPos=0;
    #while(ZPos<100)
        #declare FCount=0;
        #while(FCount<int(10*rand(SNum)))
            object{Floor
                translate <XPos,FCount*8,ZPos>
            }
            #declare FCount=FCount+1;
        #end// while fcount
        #declare ZPos=ZPos+10;
    #end// while zpos
    #declare XPos=XPos+10;
#end// while xpos

-- 
Christopher James Huff - Personal e-mail: chr### [at] yahoocom
TAG(Technical Assistance Group) e-mail: chr### [at] tagpovrayorg
Web page: http://chrishuff.dhs.org/


Post a reply to this message

From: CMcCabe
Subject: Re: arrays and random numbers
Date: 10 Apr 2000 19:50:29
Message: <38F2685A.AEFB26EF@fcmail.com>
Can't thank you enough for yours and everyone else's help.  I guess I'm
rustier than i thought on my programming or I would have picked up that I
needed to reinitialize some of the loop control values.

As for the differences between your code and mine...  wow, it's amazing that
I came that close and yet the effect of each was so different.....

Thanks all,
CMcCabe


Post a reply to this message

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