ilmenator Posted February 21, 2006 Report Share Posted February 21, 2006 Hi all,I need to create random values in the range 1-10 or so in my application. Is there an easy way to achieve this within the PIC?Thanks, ilmenator Quote Link to comment Share on other sites More sharing options...
moebius Posted February 21, 2006 Report Share Posted February 21, 2006 Well.. Many people have found this secret "hidden" function, by using compiled Midibox64 application and failing to reading the documentation :DI 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? ;DMoebius Quote Link to comment Share on other sites More sharing options...
stryd_one Posted February 21, 2006 Report Share Posted February 21, 2006 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 returnScope 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_AddSEQ_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 returnSEQ_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! Quote Link to comment Share on other sites More sharing options...
ilmenator Posted February 21, 2006 Author Report Share Posted February 21, 2006 Okay,thanks for that piece of code - I wouldn't have found it, as I have no MBSEQ :PNow, 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 Quote Link to comment Share on other sites More sharing options...
TK. Posted February 21, 2006 Report Share Posted February 21, 2006 The "sid_random" application gets use of an external "real random" generatorIn 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.