Jump to content

create random values on PIC?


ilmenator
 Share

Recommended Posts

Well..

Many people have found this secret "hidden" function, by using compiled Midibox64 application and failing to reading the documentation :D

I don't know the software solution (see the examples), but if you leave analog input pin floating and read the value and scale it down to 1-10, would that be random enough?  ;D

Moebius

Link to comment
Share on other sites

There's already a RNG in MBSEQ :)

;; --------------------------------------------------------------------------

;;  This function generates a new random number

;;  OUT: new random number in SEQ_RANDOM_SEED_[LH]

;; --------------------------------------------------------------------------

SEQ_CORE_GenRandomNumber

SET_BSR SEQ_BASE

movf SEQ_RANDOM_SEED_L, W, BANKED

mulwf SEQ_RANDOM_SEED_H, BANKED

movf TMR0L, W

addwf PRODL, W

movwf SEQ_RANDOM_SEED_L, BANKED

movlw 0x69

addwfc PRODH, W

movwf SEQ_RANDOM_SEED_H, BANKED

return

Scope the Humanizer functions out for a scaling example:

;; --------------------------------------------------------------------------

;;  this function applies a random value to a byte

;;  IN: pointer to byte in FSR1

;;      intensity in TMP1

;;  OUT: modified byte

;; --------------------------------------------------------------------------

SEQ_HUMANIZE_Modify

call SEQ_CORE_GenRandomNumber ; generate new random number

;; scale depending on intensity

rlf TMP2, W

rlf WREG, W

rlf WREG, W

andlw 0x78

mulwf SEQ_RANDOM_SEED_L, BANKED ; scaled result in PRODH

;; add/subtract depending on SEQ_RANDOM_SEED_H[0]

BIFCLR SEQ_RANDOM_SEED_H, 0, BANKED, rgoto SEQ_HUMANIZE_Modify_Add

SEQ_HUMANIZE_Modify_Sub

movf PRODH, W

subwf INDF1, W

;; saturate on overflow

IFSET WREG, 7, movlw 0x00

movwf INDF1

;; value should never be 0x00

movf INDF1, W

skpnz

incf INDF1, F

return

SEQ_HUMANIZE_Modify_Add

movf PRODH, W

addwf INDF1, W

;; saturate on overflow

IFSET WREG, 7, movlw 0x7f

movwf INDF1

return

:) TK beat us to it again!

Link to comment
Share on other sites

Okay,

thanks for that piece of code - I wouldn't have found it, as I have no MBSEQ  :P

Now, I don't know much about Assembler, what does the code do? What is the range of the values that are created? How can I use it in my C code?

In my app generating the random values is not a real-time issue, so is there any other piece of [tt]C[/tt] code that might be easier to understand for me and which does the same?

Sorry for my ignorance, I'm still learning...  ::)

Thanks a lot, ilmenator

Link to comment
Share on other sites

The "sid_random" application gets use of an external "real random" generator

In C, the "software random generator" in SEQ_CORE_GenRandomNumber function would look similar to this:
[code]
unsigned char random_seed_l;
unsigned char random_seed_h;

void gen_random_number(void)
{
  unsigned int tmp = random_seed_l * random_seed_h;
  random_seed_l = TMR0L + (unsigned char)(tmp & 0xff);
  random_seed_h = 0x69 + (unsigned char)(tmp >> 8);
}
[/code] Maybe with some additional XOR operations the random value distribution could be improved, but I'm not an expert on this topic... Best Regards, Thorsten. [/code]
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...