-
Lets set this piece by piece.
1. RandomSeed in my FINAL example is a WORD (16 bits) because the RANDOM command works on a Word variable, so we need a WORD.
2. RandomSeed.Lowbyte only acts on the Lower 8 Bits (the LOW BYTE of the Word - yes it's in the manual). Just as RandomSeed.HighByte would act on the upper Byte (upper 8 bits of the word).
3. EEPROM locations are BYTE sized (8 Bits).
4. When the PIC powers up, RandomSeed=$0000.
5. 1st time around we go to EEPROM addess Zero, and load the LOWBYTE of the RandomSeed Word with the contents. The initial Contents will be zero.
6. In my example we then add 23 to it. RandomSeed is now 23.
actually where I wrote...
RandomSeed.Lowbyte=RandomSeed.Lowbyte+23
I could just have left it as...
RandomSeed=RandomSeed+23
because it makes no difference in the scheme of things as adding 23 WILL act on the Lowbyte anyway!
7. We save the lower 8 bits (LowByte) back to EEPROM. EEPROM location zero now has 23 in it.
8. We now use 23 to itterate our first RANDOM function.
9. We only need the bottom four bits which will give us a 0-15 range.
---
Next Evening... we get to step 5 above and EEPROM address had 23 in it. We add another 23 and save the value 46 to EEPROM. We now have the value 46 to seed our RANDOM instruction.
---
On the third evening the seed will be 69... and so forth.
---
However, eventually we will spill out of a BYTE (8 bits, but we will only save the lower 8 bits of the RandomSeed word (we don't need any more than that) to be our starting point for the next day.
----
Your example is erroneous because RandomSeed is a WORD and you cannot READ or WRITE a WORD to EPROM in a single operation... you have to split it into two bytes and save each byte into separate EEPROM addresses. But like I said above, in your application saving the just lower 8 bits suffices.
-----
Once you have a variable holding a value 0-15, you can easily convert it to a 16-bit variable for shifting out of your chosen I/O to your peripheral driver.
-
Ignoring all of this for a second.
Am I right regarding the RANDOM generation and seeding? As in, the variable contents is the seed and using two different seeds would change the result? Did I get that much right anway? :-)
lowbyte as one word isn't in the manual. low and byte as separate words are, of course so what section should I be looking in to see this? I'm using the manual for PicBasic downloaded from the site where it can be purchased.
I may need to understand lowbyte and highbyte to make this work, but I'm not 100% sure since I don't care if it generates 0 to 15 or 1 to 65525 since I'm going to boil the results down to 0 and 1 anyway (at least that's my theory for now).
I still have the headache.
Bart
-
Bartman >lowbyte as one word isn't in the manual. low and byte as separate words are, of course so what section should I be looking in to see this? I'm using the manual for PicBasic downloaded from the site where it can be purchased.
In a word you have 16 bits or 2x8 bits or 2x1 BYTE. LOWBYTE and HIGHBYTE are the 2 separate BYTE
MyWORD = %1111000000001111
MyByte1=MyWORD.HIGHBYTE
MyByte2=MyWORD.LOWBYTE
===========================
MyByte1 will return 11110000 Binary or F0 hex
MyByte2 will return 00001111 Binary or 0F hex
LOW is a function to access PORT pins. LOW will place pins, bits to low level.
LOW PORTA.0 ;will place PORTA RA0 pin to low level
LOW PORTA ;will place all PORTA pins to low level
===================================
RANDOM is a function of PICBASIC who provide you a pseudo-random value in a WORD size variable, 16 bits, range from 0-65525. So if you want to use it in your application, you'll need to use a range from 0-15. In this case you'll need only 1 BYTE to give you range from 1-255, after that reduce the range to 0-15.
SO...
RANDOM MyVar
Range0_255=Myvar.LOWBYTE
Range0_15=Range0_255 & $0F ; logical operation to isolate the 4 Less Significant Bits
ex:
Range0_255 = %10101010
Range0_15=Range0_255 & $0F
Range0_15 will return 1010 Binary , A in hexadecimal, 10 in decimal
-
Okay. I see what you are talking about. I did see in the manual that a word (W0) is made of two bytes being B0 and B1. That is all well and to some extent I understand it. Two Bytes are needed to hold a HEX word since HEX is twice the size of a byte.
That still doesn't explain where the words lowbyte and highbyte come from to use them the way they are being used in the examples. How would I know this reading the book that these words exist as "lowbyte" and "highbyte" after something else? right now I feel like there is a hidden page of information I'm not seeing.
I *think* I actually grasp the pin HIGH and LOW stuff as that is where I originally started with understanding how I had to drive the multiplexer.
I'm still curious if my understanding of RANDOM is how RANDOM works. Let's get to that then I'll tackle the high and low bit stuff.
Bart
-
bartman>That still doesn't explain where the words lowbyte and highbyte come from to use them the way they are being used in the examples. How would I know this reading the book that these words exist as "lowbyte" and "highbyte" after something else? right now I feel like there is a hidden page of information I'm not seeing.
in my PBP Pro V2.45 book it's at the page 23.
Bartman>I'm still curious if my understanding of RANDOM is how RANDOM works. Let's get to that then I'll tackle the high and low bit stuff.
IMO you dont need to know how it's working since it's doing what you want :)
RANDOM is a built-in function that comes with PICBasic PRo & PicBasic standard who generate Pseudo random numers. This is all you need to know.
-
I bet you'd be hard pressed to find those words in the PicBasic manual. I'm not looking at the Pro version. Is it a Pro command that doesn't translate to the cheap version?
I don't need to know the math that happens with Random, but I still want to know how it is seeded and how that seeding affects thing. It matters to me since it helps me understand how I'm going to eventually use this idea to my ends. AND, at least I know if I have some grasp of what I've been trying to say in the above examples.
Bart
-
RANDOM is available for both compilers.
-
I'm referring to "Lowbyte" and "highbyte". Those terms are NOT used in the cheap version manual. I just looked them up in the PRO version and yes, they are there.
Since I am NOT using the PRO version I'm basing everything on the basic, ie, cheap, version of the program. Hence my total confusion as to what to do with these words in my examples.
So, I assume that either there is different terminology for the basic program or it doesn't work in the basic program? I can go back and look again now that I see what it is an alias for.
RANDOM itself is fine I just want to know how the seeding part affects the outcome. Call it curiosity on the road to understanding.
Bart
-
O.k, now i see... stupid french i am...
if you don't have HIGHBYTE and LOWBYTE function in standard PICBASIC, i suppose you'll be able to do it like this.
MyRandom VAR WORD
MyResult VAR BYTE
RANDOM MyRandom
MyRandom=Myrandom & $000F
MyResult = MyRandom
-
I keep forgetting that not everyone has PBP...
Unfortunately not having PBC I'm relying on the manual only...
RandomSeed is W0
I don't know if B0 or B1 is the Lowbyte... let's say it's B0
LightSelector is B2
so the code would go something like...
Read 0,B0
W0=W0+23
Write 0,B0
Loop:
RANDOM W0
B2=W0 & $000F
Gosub BlinkyLight
Goto Loop
Does this make things any clearer?
-
Okay, now I have some idea of what is being spoken here! Thank you.
But can my question about seeding RANDOM be answered so I can give all this more consideration to get to the guts of the matter?
Thanks.
Bart
-
Random works thus...
You provide RANDOM with a WORD variable, the contents of which is a number anywhere between 0-65535. This number is your 'seed'.
When RANDOM executes it performs an itteration on the number you have provided, and returns with another number (in the same variable) which to all intents and purposes will be totally random. Your initial variable is replaced by a new one.
The fact is if the seed is the same, then the result is the same (ie it's a repeatable random itteration) known as pseudo random (rather than true random). You can then use this result for the next RANDOM call, etc etc.
According to MeLabs, their RANDOM instruction will never return a zero result.
Melanie
-
Okay. So that is why the sequence will always be the same with RANDOM when it starts up because it is always pulling a ZERO as the seed number if left alone because no other variable has been defined?
So, taking this one step further. IF I had 5 of the exact same programs running on 5 identical projects, but the projects were not connected in any way and I could power them all at the exact same time then each of them would create random numbers in the same order?
If I wanted to really make things even more random (from the observers perspective) I could add a RANDOM number to the RANDOM variable that we are speaking of instead of using the "+23" and saving that? In effect not ever knowing what the darn thing is going to add and save in eeprom for the next time? Granted this would be more "random" than I need, but once again, it helps me understand the theory.
I'm at work so I can't totally put my mind to the high and low byte issue right now, but now that I'm seeing the applicable syntax it isn't all that much of a stretch to grasp it.
Thanks.
Bart
-
If you enter RANDOM with the same number (be it zero or otherwise) it will always produce the same repeatable sequence.
If you had your five projects, all identical, all powered up on the same day for the first time, they will all produce the same result. To ensure that didn't happen, ensure the contents of EEPROM location zero differs for each project (see DATA statement), or power each PIC on/off a different number of times (since location zero alters each time) and the projects will then all be following a different switching pattern.
Yes you could add a Random result instead of 23 as you say, but it won't make it any more random. This is because we are going to cycle through 256 possible Random start sequences anyway (as EEPROM location zero is a BYTE variable), and therefore a BYTE only has 256 possble states with which we can start seeding.
-
Yes, things are clearing up.
Now, given that eeprom is only storing a BYTE and not a WORD I can understand that generating a RANDOM WORD to try and stuff into the eeprom is pointless, but how is adding 23 any less pointless? If the eeprom is storing a BYTE of 0 and 1's isn't 23 a WORD? I know that 23 is well within the 256 limit, but is there some internal math going on that is saying, "okay, I'm going to convert that 23 to binary to make up my byte then add it to what is there already" that it CAN'T do with 65536 which is beyond the 256 limit?
So, I also assume that to stuff a WORD into eeprom there is a way to separate the low and high bytes and put into two eeprom addresses?
This is getting past the random issue now. I am looking at the inner workings of word and byte.
Further. Is this written correct? You wrote:
Read 0,B0
W0=W0+23
Write 0,B0
As I see this we are just putting the same information back into address 0 without really adding anything to it since the +23 is going on a word variable. Is line two possibly b0=w0+23 then only the lowbyte is being written back?
Bart