Random function - How to limit it ??


Closed Thread
Results 1 to 19 of 19
  1. #1
    martarse's Avatar
    martarse Guest

    Default Random function - How to limit it ??

    Hello,

    I want to know if it is possible to use the RANDOM function under a limited range.

    Let me explain :

    I want to use the RANDOM function and the BRANCH to make 39 differents things (branch to 39 differents labels to execute differents functions).

    There is the code

    RANDOM result
    BRANCH result,[state1, state2, state3, state4,...,state39]

    This is equivalent to do this
    If result=0 go to state1 subroutine
    if result=1 go to state2 subroutine
    ...

    The RANDOM function give me 0 to 65535 results, how can I limit it to 0-38 for my 39 states ??

    thanks for your help!

    Martin

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Lightbulb

    Bonjour Martin,

    ( 65535 / 1640 ) - 1 = 38.96 ... cqfd !!!

    Comme le PIC travaille avec des nombres entiers ... le problème des décimales est tout résolu.

    Tu peux mème éventuellement relancer le calcul si tu obtiens deux fois de suite le mème nombre ...

    Alain

    PS : The 39 steps ... take care !!! ( humour ...)

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    easy way is to mask results

    if you mask with 3F you will get a max of 63 combination. may few lines of if then else can do the rest of the job....

    RANDOM MyRandomVar
    MyRandomVar=MyRandomVar & $3F

    if MyRandomVar>39 then
    MyRandomVar=MyrandomVar-39
    endif

    that's it!!!
    Last edited by mister_e; - 28th November 2004 at 22:50.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    Merci Alain, Je vais essayer cela.

    Steve, can you explain me more in detail how the bitwise (&) function work ?

    thanks,

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Monsieur Martin,

    It's all based on Logical operation.
    1 AND (&) 0 =0
    1 AND (&) 1 =1


    3F = 11 1111

    let's say MyRandomVar return 23121

    23121 = 101 1010 0101 0001

    3F = 000 0000 0011 1111

    result = 000 0000 0001 0001 = 11 Hex = 17 Decimal
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    Hello Steve,

    You have updated your post after my reply.

    now I understand,

    I don't know why I was not able to imagine this way during the last night...perhaps it was too late in the night and my brain was not working properly! hi!

    thanks for your help!!

    Martin

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I don't know why I was not able to imagine this way during the last night...perhaps it was too late in the night and my brain was not working properly! hi!
    NOWAY!!! maudite boisson


    P.S.: for the benefit of all here maudite boisson... mean too much beer!!!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    hihiih!

    Thats right! do not mix work with beer...the result are not very good.

    thanks all for your reply everything work here!

  9. #9
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default Random is not a real random

    Hello,

    I've got another question about the Random function.

    Is it a real random function that comes with PBP ??

    I've write every result of the Random function (with the 0-38 trick listed above) in the internal EEPROM of my PIC16F84A and I found that the result seems to follow a patern.

    Here are the results of the random function (in Hex format)
    18 0A 15 03 07 0E 1C 11 23 07 0E 1C 11 23 07 0E 1C 11 23 07 0E 1C 11 23 07 0E 1C 11 23 07 0E 1C 11 23 07 0E 1C 11 23 07 0E 1C 11 23 07....

    the patern seems to be : 07 0E 1C 11 23 (Hex) or 7 14 28 17 35 in decimal format.

    Everything seems to be good on the first results but after some loop the results seems to follow a patern. I also found that every time I reset the chip, it start with the same result : 18, 0A, 15, 03...

    Is there a method to get a "real" random ?

    thanks
    Last edited by martarse; - 29th November 2004 at 07:06.

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink

    Bonjour Martin,

    Je n'ose croire que les deux méthodes donnent le mème résultat !!!
    Les as-tu essayées toutes deux ???

    En ce qui concerne le Hasard, il s'agit là plus d'une question philosophique qu'autre chose:

    NON, le Hasard n'existe pas ... il n'y a que des conjonctions de facteurs qui font que. Et notre petit cerveau est trop limité pour arriver à mettre en équation une telle quantité de facteurs ...
    Il n'existe aucun algorithme aléatoire ...pour la bonne raison qu'il a été conçu au départ par calcul, aussi complexe qu'il soit !!!

    Donc, avec un PENTIUM IV, l'algorithme est seulement plus complexe que sur nos PIC's ...

    Un petit tour dans PbpPic14.lib te permettra de voir comment est programmée en assembleur la fonction "RANDOM"

    ;************************************************* ***************
    ;* RANDOM : Generate random number *
    ;* *
    ;* Input : R0 = seed *
    ;* Output : R0 = result *
    ;* *
    ;* Notes : 24 cycles including call and DONE. *
    ;************************************************* ***************

    ifdef RANDOM_USED
    LIST
    RANDOM movf R0 + 1, W ; 1 Assure Seed <> 0
    iorwf R0, W ; 1
    btfsc STATUS, Z ; 1 / 2 Nudge Seed to 1
    incf R0, F ; 1 / 0
    movlw 80h ; 1 Tap Bit 15
    btfsc R0, 4 ; 1 / 2 Tap Bit 4
    xorwf R0 + 1, F ; 1 / 0
    btfsc R0, 2 ; 1 / 2 Tap Bit 2
    xorwf R0 + 1, F ; 1 / 0
    btfsc R0, 1 ; 1 / 2 Tap Bit 1
    xorwf R0 + 1, F ; 1
    rlf R0 + 1, W ; 1 C = New Bit
    rlf R0, F ; 1 Rotate Left
    rlf R0 + 1, F ; 1
    goto DONE ; Done
    NOLIST
    DONE_USED = 1
    endif


    La question était tout de mème surprenante, venant d'un enseignant ...

    "Seuls l'algèbre et la géométrie sont des sciences exactes, tout le reste n'est qu'approximation plus ou moins fine ..."

    Amitiés
    Alain

  11. #11
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    Salut Alain,

    non, les deux méthode n'arrivent pas à la même chose mais les deux arrivent à des résultats qui bouclent sur eux-même.

    En fait, je me suis mal exprimé, mon anglais en arrache un peu.

    Je voulais plutôt dire un random plus complet ou complexe que cela. Je suis bien d'accord qu'après 1000 ou 2000 le patern soit répétitif...mais pas 5 ! hihi!

    Un peu trop petit...je ne passe même pas à travers mes 39 sous routine! hi!

    je vais regarder ca plus en détail et au pire, patcher un bout en assembleur qui permettrait un patern plus complexe.

    merci,

  12. #12
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hi Matarse and Acetronics. That's a fact that there's nothing pure RANDOM in the world... even everything is made by machine who operate by logical operation.

    RANDOM is close to be a real RANDOM but, in the Matarse's case, we have to limit the range by ANDings, this is maybe why some results comes up more often than others

    255 & 63 = 63
    127 & 63 =63
    4095 & 63 =63

    etc...

    if you want to avoid some possible combination.. why not store one serie in eeprom, internal one or external, and see if already comes, in case yes, redo random....

    regards...

    P.S.: PLEASE, since not everybody 'round here speak french, for the respect of the forum rules and peers, post in english. Some other can really help you. But if they don't understand... what is the chance to get help from them! Other than english can/must be post in private... i think.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  13. #13
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Steve,

    Steve>>P.S.: PLEASE, since not everybody 'round here speak french,<<

    Thanks Steve. My only language is English and Sign Language. I can 1/2 way pick apart French and Spanish and get the "drift", but I do not read or speak either language.

    But I am happy that the Gent got his answer, and things are working will for him.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  14. #14
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Lightbulb

    Coucou Martin,

    Je la tiens l'erreur !!!

    En fait, il faut donner une "graine" comme tu as pu lire dans l'assembleur... ben, la graine correspond à ta variable elle mème
    Donc : commence par donner une valeur quelconque à ta "randomVar" ...
    une solution est de faire un premier Random, et d'utiliser ensuite le dernier résultat comme "graine" ...
    Pense éventuellement à le mémoriser en cas de coupure de courant ...

    là, ce n'est pas étonnant, tu commences toujours avec la mème valeur ( je parierais 0 ...)

    Yeah, my english is not so cute ... on les aura, les tuniques rouges !!!

    Alain

  15. #15
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Talking

    Hi Dwaine,

    Shortly, a value must be given to "my Random var" to make algorithm go well ...
    Let's have at first one RANDOM to get a seed , and a new one to get the Random number.
    And after re-use the result as a seed.

    That's it in English ...

    Just for you ...

    Alain

  16. #16
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Acetronic and MisterE,

    I'll try these new ways and I'll let you know if something works good for me.

    Martin

    PS: sorry for my french Post, I'll stay in english for my new post.

  17. #17
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    There is a ASM code for Random I've found on the micrchip website

    If the other methods do not work, I'll try to include this ASM on my PBP code.

    I'll let you know.


    ;Random Number Generator
    ;
    ; This routine generates a 16 Bit Pseudo Sequence Random Generator
    ; It is based on Linear shift register feedback. The sequence
    ; is generated by (Q15 xorwf Q14 xorwf Q12 xorwf Q3 )
    ;
    ; The 16 bit random number is in location RandHi(high byte)
    ; & RandLo (low byte)
    ;
    ; Before calling this routine, make sure the initial values
    ; of RandHi & RandLo are NOT ZERO
    ; A good chiose of initial random number is 0x3045
    ;************************************************* ******************
    ;
    Random16
    rlcf RandHi,W
    xorwf RandHi,W
    rlcf WREG, F ; carry bit = xorwf(Q15,14)
    ;
    swapf RandHi, F
    swapf RandLo,W
    rlncf WREG, F
    xorwf RandHi,W ; LSB = xorwf(Q12,Q3)
    swapf RandHi, F
    andlw 0x01
    rlcf RandLo, F
    xorwf RandLo, F
    rlcf RandHi, F
    return

  18. #18
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Acetronics,


    A>>Shortly, a value must be given to "my Random var" to make algorithm go well ...
    Let's have at first one RANDOM to get a seed , and a new one to get the Random number.
    And after re-use the result as a seed.

    That's it in English ...

    Just for you ...<<

    ROFL! I was randomly picking out words that I know...Yeah it made sense <g> with the help of your expertise translation.

    Here, I will send a message to you:

    (Dwayne waving his hands in Sign language).

    Dwayne

    PS..I couldn't say it any louder, and they didin't have a font or graphical animation to interpret it <g>
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  19. #19
    Join Date
    Nov 2004
    Location
    Saskatchewan Canada
    Posts
    189


    Did you find this post helpful? Yes | No

    Default

    This all sounds very familiar since I just went through enough random issues to write a book.

    Check out this thread for an idea on how to ensure it doesn't start out the same way each time.

    http://www.picbasic.co.uk/forum/show...5&pagenumber=1

    When combined with some other code to prevent the same number from coming up twice in a row the output sequence takes on a fairly decent random appearance.

    Bart

Similar Threads

  1. Questions on random numbers and multiplexing LEDs
    By AaronM in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th June 2009, 04:22
  2. Random Numbers
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 18th December 2008, 08:47
  3. 6 random numbers limiting to 49 ?????
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th June 2005, 23:27
  4. Random number results
    By bartman in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th March 2005, 17:39
  5. more random number questions
    By bartman in forum mel PIC BASIC
    Replies: 1
    Last Post: - 14th November 2004, 17:55

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

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