Menu

Random number very small range problem

Help
viscomjim
2016-06-14
2016-06-15
  • viscomjim

    viscomjim - 2016-06-14

    Using the version 0.95.006 of GCB. Trying to get a decent range using the random command...

    This is what I am doing

    #chip 16F1825, 32
    
    #DEFINE USART_BAUD_RATE 9600
    #define USART_BLOCKING
    
    'Dir PORTC.0 In   'AD port AN4 pin 10
    
    randomize 2341 'ReadAD(AN4)
    
    do
    rn = Random
    HSerprint rn
    wait 200 ms
    loop
    

    The numbers I get for variable rn range from 48 to 57 and that is it. I tried using the AD for randomize and all sorts of numbers, but still same results. I really need to see numbers in the 0 to 255 range as indicated in the manual. What am I doing wrong here?

    Thanks!!!

     
  • David Stephenson

    Not sure what is happening here, but 48 to 57 are the ASCII values for the digits 0 to 9

     
    • Anobium

      Anobium - 2016-06-14

      @David. Good point.

      @viscomjim. Try HSerprint str(rn), and, please change the randomize 2341 as this will always seed the same value, and, you may get the same sequence. Refer to the Help file for a working example.

       
  • viscomjim

    viscomjim - 2016-06-14

    Hi and thanks for the reply, however, I used different seed values and even used reading the floating adc pin for randomize and still get the same range of numbers, they are not in the same order, just the same range. I'm trying to get a larger range. Any ideas? I did use the help file example with the same results. Not sure what to do...

     
  • mmotte

    mmotte - 2016-06-14

    viscomjim

    function myrnd
    RLF myrndnum,W
    RLF myrndnum,W
    BTFSC myrndnum,4
    XORLW 1
    BTFSC myrndnum,5
    XORLW 1
    BTFSC myrndnum,3
    XORLW 1
    MOVWF myrndnum
    myrnd = myrndnum

    end function

    function getRndLtr
    Loop1:
    Letter = myrnd AND 0x7F
    if Letter > 90 then goto Loop1
    if Letter < 65 then goto Loop1
    getRndLtr = Letter

    end function

    function getRndNum
    Loop2:
    Letter = myrnd AND 0x7F
    if Letter > 57 then goto Loop2
    if Letter < 48 then goto Loop2
    getRndNum = Letter
    end function

    These worked pretty good several years ago.
    73
    M

     
    • Anobium

      Anobium - 2016-06-14

      @mmotte. I will investigate this RANDOM issue as this should work as documented.

       
  • Anobium

    Anobium - 2016-06-14

    I will check using the latest compiler. I will be using the same chip class so this should be a valid test.

     
  • viscomjim

    viscomjim - 2016-06-14

    Hi mmotte, that functions looks pretty cool. I will have a go at it to see the results. Evan, thanks for taking a look, I hope I didn't make a bonehead mistake. I look forward to your findings.

     
    • Anobium

      Anobium - 2016-06-14

      Using the latest build. I got. This looks correct.

      Random Test
      138
      135
      108
      24
      165
      71
      97
      121
      226
      174
      80
      252
      130
      141
      154
      201
      37
      235

      From this program

          #chip 16f18855,32
          #Config FEXTOSC_OFF, RSTOSC_HFINT32
          #Config WRT_OFF, CPD_ON, MCLRE_ON
      
      
            'Set the PPS of the ports.
            UNLOCKPPS
              'USART
              RC0PPS = 0x0010       'RC0->EUSART:TX;
              RXPPS  = 0x0011         'RC1->EUSART:RX;
      
            LOCKPPS
      
            'USART settings
            #define USART_BAUD_RATE 115200
            #define USART_BLOCKING
            #define USART_TX_BLOCKING
      
        ; ----- Define Hardware settings
          dir porta out
      
        ; ----- Constants
      
        ; ----- Variables
      
        ; ----- Quick Command Reference:
      
        ; ----- Main body of program commences here.
            HSerPrint "Random Test"
            HSerPrintCRLF
      
            'Use randomize, with the value on PORTD as the seed
            Randomize PORTA
      
            'Generate random numbers, and output on PORTA
            Do
                PORTA = Random
                HSerPrintByteCRLF porta
                Wait 1 s
            Loop
      
       

      Last edit: Anobium 2016-06-14
  • viscomjim

    viscomjim - 2016-06-14

    What the heck... I need to try again! I can't see what I am doing wrong here. Will keep you posted once I get back to my setup. Jeez....(scratching head)

    I noticed in your code you set "Randomize PORTA" instead of PORTD, can you give that a try and see if you get the same type of results?

    Thanks

    Jim

     
  • Anobium

    Anobium - 2016-06-14

    Next test also looks good.

    Random Test
    128
    238
    222
    60
    255
    208
    42
    227
    63
    32
    112
    130
    167
    58
    157
    218
    92
    105
    217
    239
    77
    76
    239
    51
    172
    167
    51
    117
    224
    254
    56
    142
    162
    94
    112
    201

    Using code on a 16f1829

          #Chip 16f1829, 32
    
          'USART settings
           #define USART_BAUD_RATE 9600
           Dir PORTc.6 Out
           Dir PORTc.7 In
           #define USART_DELAY 5 ms
           #define USART_BLOCKING
           wait 500 ms
    
      ; ----- Define Hardware settings
        dir portc out
    
      ; ----- Constants
    
      ; ----- Variables
    
      ; ----- Quick Command Reference:
    
      ; ----- Main body of program commences here.
          HSerPrint "Random Test"
          HSerPrintCRLF
    
          'Use randomize, with the value on PORTD as the seed
          Randomize PORTA
    
          'Generate random numbers, and output on PORTA
          Do
              PORTC = Random
              HSerPrintByteCRLF portC
              Wait 1 s
          Loop
    
     
  • Anobium

    Anobium - 2016-06-14

    Same code as above using the 0.95 2016-02-24 build, also look ok on the 16f1829.

    I used the Low Pin Count Board and the LEDs are flashing away reflecting the value displayed on the terminal.

    Random Test
    217
    35
    146
    26
    92
    156
    66
    12
    180

     
  • Anobium

    Anobium - 2016-06-14

    I also removed as much code as practical and all seems ok. I removed the additional HSERserial commands, delays etc. Tested ok 16f1829.

    If it still fails, post a ZIP with the source, the asm ... everything really. :-)

     
  • viscomjim

    viscomjim - 2016-06-14

    thanks Evan for checking that. When I get back to my setup, I will give this a whirl again. Sorry for taking your time. Will post back!

     
  • viscomjim

    viscomjim - 2016-06-15

    Well, I'm a ding dong. Code works as it should. I made a STUPID mistake on my setup and all is well now. Sorry for the time waster.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.