Shop OBEX P1 Docs P2 Docs Learn Events
What is this structure: w[something]... — Parallax Forums

What is this structure: w[something]...

Hello,

I am muddling through .spin and would like to know what this structure represents, in plain English please.

w[btm_line]:=((?rand & $07) * 8)

I am familiar with BASIC from the 80's and this statement has me stumped. Clearly something (an array?) is being defined as being equal to a random number, which is logically combined to $07 for some reason, etc . Any help would be appreciated.

Thank you.

KB

Comments

  • dgatelydgately Posts: 1,630
    edited 2019-01-05 00:18
    Propeller Manual v1.2, Page 159:
    Random ‘?’
    The Random operator is a special, immediate operator that uses a variable’s value as a seed to create a pseudo random number and assigns that number to the same variable. It can only be used in run-time variable expressions. Random has two forms, forward and reverse, depending on which side of the variable it appears on. The forward form appears to the left of a variable and the reverse form appears to the right of a variable.
    Random generates pseudo-random numbers ranging from -2,147,483,648 to +2,147,483,647. It’s called “pseudo-random” because the numbers appear random, but are really generated by a logic operation that uses a “seed” value as a tap into a sequence of over 4 billion essentially random numbers. If the same seed value is used again, the same sequence of numbers is generated. The Propeller chip’s Random output is reversible; in fact, specifically it is a 32-bit maximum-length, four-tap LFSR (Linear Feedback Shift Register) with taps in both the LSB (Least Significant Bit, rightmost bit) and the MSB (Most Significant Bit, leftmost bit) allowing for bi-directional operation.
    Think of the pseudo-random sequence it generates as simply a static list of over 4 billion numbers. Starting with a particular seed value and moving forward results in a list of a specific set of numbers. If, however, you took that last number generated and used it as the first seed value moving backward, you would end up with a list of the same numbers as before, but in the reverse order. This is handy in many applications.
    Here’s an example:
    ?X
    The above shows the Random forward form; it uses X’s current value to retrieve the next pseudo-random number in the forward direction and stores that number back in X. Executing ?X again results in yet a different number, again stored back into X.
    X?
    The above shows the Random reverse form; it uses X’s current value to retrieve the next pseudo-random number in the reverse direction and stores that number back in X. Executing X? again results in yet a different number, again stored back into X.
    Since Random is always an assignment operator, the rules of Intermediate Assignments apply to it (see page 147).
    
    So, your "?rand" is a "Random forward form", using the value of the rand variable to generate a new pseudo-random number. The "& $07" logically ANDs the rand variable value with $07, which is a binary 00000111 (masking the 3 rightmost bits).

    dgately
  • ?rand is the random prefix operator. It takes the current value of the variable rand and scrambles it using a tapped linear shift register algorithm storing that back into rand. That pseudo-random value is masked to produce a value between 0 and 7 (that's the "(?rand & $07)").

    The rest should be straightforward if you remember that the assignment statement is written using ":=" rather than just an equal sign.
  • "w[ i ]" is an array access; it represents the i'th element of the array w. In BASIC we write this as "w( i )".
  • Thank you gents, I'm clear now on the rand portion, makes sense. My remaining query is that of the mechanism of the "w" and square brackets. Mike, you are probably facepalming now, sorry. The propeller book I have says only ":= Assignment" which isn't so helpful.

    KB
  • The value of w[btm_line] is being randomized between 0 and 7.
  • Thank you gents, I'm clear now on the rand portion, makes sense. My remaining query is that of the mechanism of the "w" and square brackets. Mike, you are probably facepalming now, sorry. The propeller book I have says only ":= Assignment" which isn't so helpful.

    KB

    "x := y" changes the variable (or array element) x to have the value y.

    Here's how to initialize an array so that each element is equal to 0:
    VAR
       long a[8]  ' an array of 8 longs
    PUB cleara | i
      repeat i from 0 to 7
        a[i] := 0
    
    This would be written in BASIC as (I've used the fastspin dialect, since that's one of the BASICs that runs on the propeller):
    dim a(7) ' an array of 8 integers, indexed from 0 to 7
    
    sub cleara
      dim i
      for i = 0 to 7
        a(i) = 0
      next i
    end sub
    
  • JonnyMacJonnyMac Posts: 9,102
    edited 2019-01-05 01:29
    While you can initialize an array with a loop, if all elements are the same value you can use one of the xxxxfill instructions.
      longfill(@a, 0, 8)
    
    Of course, there is also bytefill and wordfill.
    The propeller book I have says only ":= Assignment" which isn't so helpful.
    Spin borrows Pascal's assignment operator for variables. For constants as single = sign is used. To test for equality, use ==. For example:
      z := x == y
    
    This will set z to true if x and y are equal, false otherwise.
Sign In or Register to comment.