PDA

View Full Version : Random function - How to limit it ??



martarse
- 28th November 2004, 08:31
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

Acetronics2
- 28th November 2004, 09:43
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 ...)

mister_e
- 28th November 2004, 22:34
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!!!

martarse
- 28th November 2004, 22:45
Merci Alain, Je vais essayer cela.

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

thanks,

mister_e
- 28th November 2004, 23:06
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

martarse
- 28th November 2004, 23:23
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

mister_e
- 29th November 2004, 00:21
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!!!

martarse
- 29th November 2004, 00:35
hihiih!

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

thanks all for your reply everything work here!

martarse
- 29th November 2004, 06:56
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

Acetronics2
- 29th November 2004, 08:06
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

martarse
- 29th November 2004, 14:43
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,

mister_e
- 29th November 2004, 15:59
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.

Dwayne
- 29th November 2004, 16:08
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

Acetronics2
- 29th November 2004, 16:11
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

Acetronics2
- 29th November 2004, 16:16
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

martarse
- 29th November 2004, 16:29
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.

martarse
- 29th November 2004, 19:16
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

Dwayne
- 29th November 2004, 19:24
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>

bartman
- 30th November 2004, 14:05
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/showthread.php?s=&threadid=849&perpage=15&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