random number in Edata


Today 15:14
Forum: Proton Plus Compiler v3
Starter: hadv215
Views: 0
Replies: 5
Yesterday 20:24
Forum: New devices and techniques
Starter: normnet
Views: 0
Replies: 1
Yesterday 20:17
Forum: Proton Users File Server
Starter: Les
Views: 0
Replies: 0
+ Reply to Thread
Results 1 to 10 of 10

Thread: random number in Edata8 days old

  1. #1
    Member amateurtje's Avatar
    Join Date
    Aug 2008
    Posts
    113
    Thumbs Up
    Received: 5
    Given: 2
    Total Downloaded
    93.58 MB

    0 Not allowed!

    Post random number in Edata

    Hello,

    I need to store only once, at the programming stage, a random number Via the Edata in the EEPROM. It needs to be a 4 digit number. So I used this code completely at the beginning of my script:
    Code:
    Dim Code As Word
    begin:
    Code = Random
    Code =Code/10
    If    Code < 1000 Then GoTo begin
    And at the EDATA section I implemented the Variable "Code".. However, it does not generate a random number but the number 65324. (A little bit) as expected, he does not run the script but puts something in the Edata.

    Anybody a good idea?

    I was thinking myself maybe:
    - When Run the software script for the first time (based on the bit in the next line), generate a random number.
    - Set a bit in the eeprom when that is done.





    Code:
    Dim rCode As Word
    
    Dim randomcodebit as bit Start: randomcodebit = eread 263 if randomcodebit = 0 then
    begin:
    rCode = Random rCode =Code/10 If rCode < 1000 Then GoTo begin ewrite 261, [rCode] Ewrite 263, [1] ;randomcodebit end if rcode = eread 261

    Also this code does not help and generates the same number every time. Even with the seed command insrted it generates every time the same number. Does anybody know how to get a random number?

    PS, The number does not need to be really random... Just a different number on every pic I program is sufficient. They also do not need to be unique… So just a pseudo random number is good enough but it does not even do that…..
    Last edited by amateurtje; 17th April 2019 at 08:56.

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Senior Member Stephen Moss's Avatar
    Join Date
    Jan 2006
    Posts
    445
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    2.50 GB

    0 Not allowed!

    Default Re: random number in Edata

    Your problem is that it is difficult to get a truly pseudo random number. I do not know how the Random instruction generates a number but any random number generator starts with a seed value, if that value is always the same then the random number (or sequence of random numbers) generated will always be the same every time the program runs. Consequently getting the initial unique random value is the difficult part, after that you could use the current value in EEPROM as the seed to ensure a new random value is generated the next time the code runs on power up/reset.

    Consequently, if trying to generate the random value in code you really need to use a unique seed value every time, in VB I use the current PC system date/time as the seed, as they are always changing that ensures a unique individual (or sequence of) random number is generated every time he program is run. How you would get a random seed value from a PIC is not clear to me, however the contents of some registers are listed as being unknown at power on/reset, how random the contents of those register are and how much it varies between devices of the same part number is unknown but perhaps using whatever value is in them as your seed will create enough random values between devices to suit your needs.
    Another possibility for obtaining a random seed value would be to read the analogue input of a floating input pin, but again how much variation you would get is unknown to me but the noisier the environment the greater the variation in reading may be - you could take several readings and add them to try and get more of a variation in values.

    The other problem of generating the value in code as I eluded to above is that very time the device powers up/resets you will get a new value which may not be what you want so either you will have to initally check the designated EEPROM location and only generate the value if it is 0 or add the random number externally after compiling.
    As you may be aware Evan(?) has recently posted on here about the PICstart replacement PICkit2/3 programming software, perhaps he could be persuaded to add such a function to that software that say lets you specify a memory location, and upon programming the first device generates a random value with a check box to use the currently generated value for successive programming (if you want to batch program several devices with the same value) or generates a new value every time so each device gets a unique random value. That would the most practical method of automatically inserting unique randoms value to compiled code at programming time that I can think of.

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,119
    Thumbs Up
    Received: 102
    Given: 24
    Total Downloaded
    2.10 GB

    0 Not allowed!

    Default Re: random number in Edata

    Some time ago Les came up with a pretty random random generator.
    Take a look in <user>\PDS\Samples24\Real Random.
    I know this example is for the 16-bit range, but I suppose it can be modified for the 8-bit devices.

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Member amateurtje's Avatar
    Join Date
    Aug 2008
    Posts
    113
    Thumbs Up
    Received: 5
    Given: 2
    Total Downloaded
    93.58 MB

    0 Not allowed!

    Default Re: random number in Edata

    Hi Hadv,

    Thanks. I took a look and started to try to re-write but it is, for somebody who never worked with these proc and result types, a bit difficult to rewrite. maybe it is easier as Stephen suggested, just to take an adress which is different for each pic to use as a seed (or even as a value in my case) . anybody a good idea for an address in a pic that is unique (or at least a bit variable) for each pic??

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    Member amateurtje's Avatar
    Join Date
    Aug 2008
    Posts
    113
    Thumbs Up
    Received: 5
    Given: 2
    Total Downloaded
    93.58 MB

    0 Not allowed!

    Default Re: random number in Edata

    is it maybe a possibility to use the internal frquency calibration? Is that an address? or is there somewhere an address with a serial number or manufacturing number of some sort?

  10. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    658
    Thumbs Up
    Received: 122
    Given: 2
    Total Downloaded
    3.55 GB

    0 Not allowed!

    Default Re: random number in Edata

    You can find a good random code in my library HERE, in the Random folder.
    [U]73's de Alberto ea3agv[/U]

  12. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  13. #7
    Member amateurtje's Avatar
    Join Date
    Aug 2008
    Posts
    113
    Thumbs Up
    Received: 5
    Given: 2
    Total Downloaded
    93.58 MB

    0 Not allowed!

    Default Re: random number in Edata

    Hi Alberto,

    I do not understand what I do wrong. It took some time to get it to work but still I do get every run the same number...

  14. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  15. #8
    Senior Member Stephen Moss's Avatar
    Join Date
    Jan 2006
    Posts
    445
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    2.50 GB

    0 Not allowed!

    Default Re: random number in Edata

    Quote Originally Posted by amateurtje View Post
    Hi Hadv,

    Thanks. I took a look and started to try to re-write but it is, for somebody who never worked with these proc and result types, a bit difficult to rewrite. maybe it is easier as Stephen suggested, just to take an address which is different for each pic to use as a seed (or even as a value in my case) . anybody a good idea for an address in a pic that is unique (or at least a bit variable) for each pic??
    The code Harm suggested is similar to my ADC suggestion in that uses bit 0 of consecutive ADC reads of a floating ADC pin to fill a byte of data (as opposed to all 8/10 bits I suggested). I am not 100% up with procedures either but they are essentially subroutines so it is relatively simple to extract the code from a procedure and just use it as normal code, in this case lifting the code from Les' code and converting to non procedure code would result in something like this...
    Code:
    Dim RandNum As Byte = 0   'Your Random Value
    Dim ADC_Result As Byte = 0   'Result of ADC reading of a floating analogue input pin 
    Dim bIndex As Byte = 8
    
    'Create random value
        Repeat
            Dec bIndex
          'Get Random Bit
            ADC_Result = ADIn 0  'Get ADC reading from a floating ADC pin 
            ADC_Result = ADC_Result & $01   'Mask off all bits other than bit 0
          'Add bit to Random number
            RandNum = RandNum << 1  'Move value in RandNum left 1 bit to make room for new bit
            RandNum = RandNum | ADC_Result   'OR value of RandNum and Result to add new bit to Random
        Until bIndex = 0
    RandNum should now contain an 8 bit random value that is hopefully difference enough from device to device to suite your requirements, replace bIndex byte & 8 for Word and 16 if you want a words value.

    Quote Originally Posted by amateurtje View Post
    is it maybe a possibility to use the internal frequency calibration? Is that an address? or is there somewhere an address with a serial number or manufacturing number of some sort?
    Some (mostly older 10, 12 & maybe some 16 series devices) do to have a calibration value for their internal RC oscillator at top of memory that could be used either as a seed or directly as your random value. However, if you are using a more modern device the internal oscillators are LASER trimmed to be accurate and only allow you to change it slightly but writing to a register instead of reading the OSCCAL value from the memory so that is not an option with those devices.

    Quote Originally Posted by amateurtje View Post
    Hi Alberto,

    I do not understand what I do wrong. It took some time to get it to work but still I do get every run the same number...
    I do not think you are doing anything wrong. I did not full understand Alberto' code so I maybe missing something but it just appears to be a method of generating a random number, however regardless of how the code generates the number if the seed value is the same every time as it appear to be in this code (MySeed($A385B341)) then as I mentioned before the numbers generated will be the same everytime.

  16. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  17. #9
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,119
    Thumbs Up
    Received: 102
    Given: 24
    Total Downloaded
    2.10 GB

    0 Not allowed!

    Default Re: random number in Edata

    The recent versions of the 8 bit compiler support procedures, just read the manual.

  18. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  19. #10
    Member amateurtje's Avatar
    Join Date
    Aug 2008
    Posts
    113
    Thumbs Up
    Received: 5
    Given: 2
    Total Downloaded
    93.58 MB

    1 Not allowed!

    Default Re: random number in Edata

    Thanks for the help. In the meantime I used a little bit different methode. The ADC methode also costs me a pin and I am trying to safe them as much as possible while I do not have a lot of them left.
    I am using the 18F22k46.
    I was thinking along the line what you said that if the seed is the same, the random number is the same. And from starting the device and letting it run, the seed will be the same. When a device is programmed, I ofcourse always power it up for the first time and my script is only for the first ower-up in its life... So, what I did,
    I just acivated the inerrupt,
    waited on a press of one of the buttons(this makes it variable), took timer0 (that runs later in the program anyway) and put that in the seed. Using the tmr0 is something I read somewhere also but I was not thinking about making it all a bit variable with a wait for a button...

    Thanks for anybody's reaction and helping me. It actually does something really small in the program but cost me about half a day or more...

    thanks all

  20. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. [SOLVED !] Need help with scaling a random number.
    By BillR in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 26th November 2015, 12:54
  2. Need a random number set
    By glenlivet in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 22nd August 2011, 18:13
  3. Any "better" random number gens out there?
    By picyerknows in forum Proton Plus Compiler v3
    Replies: 13
    Last Post: 22nd March 2008, 12:16
  4. Random number
    By all77 in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 26th October 2007, 02:45
  5. Random number
    By Roshan in forum Proton Plus Compiler v3
    Replies: 1
    Last Post: 30th April 2005, 16:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts