PDA

View Full Version : RANDOM - always the same result



flotulopex
- 16th March 2007, 11:22
Hello,

I'm doing something so obviously wrong, I can't see the tree in the forest anymore.


rvalue VAR WORD
random rvalue
R_V0 = (RValue//4) + 1 ' Convert it to range 1-4
if R_V0 = 1 then high led1
if R_V0 = 2 then high led2
if R_V0 = 3 then high led3
if R_V0 = 4 then high led4

The code above will always show "led3".

What's wrong here?

Acetronics2
- 16th March 2007, 13:59
Hi,Flotul

1) Read carefully the manual : there must be a SEED for the random function ... if always the same ... always the same result !!!

2) a little SEARCH would be THE THING to do ... good surprise for you !!!

Alain

flotulopex
- 16th March 2007, 14:19
Bonjour Alain,

Thanks...

I just found a way to make it work.

I changed my code and put R_V0 = rvalue & $03. But, to be honest, I don't understand why it works now.

Before opening this thread, I read lots of other threads about this subject. I've also been reading the Compiler's manual. I read this "Var is used both as the seed and to store the result" but my english is to poor and I don't understand the correct meaning of the word "seed" (graine - in french) here.

Could you explain this command in other words please?

mister_e
- 16th March 2007, 15:10
Kind of 'conjugaison du verbe voir => see, seed'.

Qu'est-ce qui a déja été vue, sortie, choisi, sélectionné.

flotulopex
- 16th March 2007, 17:25
j'ai l'air un peu c.. avec ma "graine" :)

a) So, when the statement is RANDOM RandomVar, the "output" value will be stored in RandomVar, no?

b) R_V0 = rvalue & $03 will keep only the 4 LSB of the result, no?

c) still don't understand why my code in post#1 doesn't work. Must I specify it is in a loop? It is and doesn't work.

mister_e
- 16th March 2007, 19:59
j'ai l'air un peu c.. avec ma "graine"
LMAO you don't want to know the meaning of the Quebec expression 'Graine' ;)



a) So, when the statement is RANDOM RandomVar, the "output" value will be stored in RandomVar, no?
Indeed


b) R_V0 = rvalue & $03 will keep only the 4 LSB of the result, no?
Nope, 3=%00000011 so only the 2 LSBs


c) still don't understand why my code in post#1 doesn't work. Must I specify it is in a loop? It is and doesn't work.

I guess you should use something like
rvalue=R_V0 before using Random again...

flotulopex
- 18th March 2007, 08:46
I have some kind of idea...

I'm coming over to CANADA next winter for holidays. Let me know which words, I European, shouldn't use ;)

I thought the "&" operator would keep (isolate) only bits 0-3 of "Value" but I was wrong. For the fours LSB, I should have written $0F.

What is then this "& $03" for?

As you suggested, I modified my code as (I've added the "Value = R_V0" in le loop):


Value Var WORD
Store_MEM:
FOR Counter = 0 to 20
RANDOM Value
R_V0 = (value & $03)
IF Mem_L > 3 THEN READ Mem_L - 3, R_V1 'Do not allow more than 3 same # in a row
IF R_V1 = R_V0 THEN STORE_MEM
WRITE Mem_L, R_V0
Mem_L = Mem_L + 1
Value = R_V0
NEXT

Nevertheless, I always get the same number sequence over again.

flotulopex
- 18th March 2007, 12:20
Is this really RANDOM?

I've been checking the sequence over and over again with 50 "random" generated numbers in a row.

The result is an exact same complete sequence everytime I power-up my PIC.

Anyone any clue?

flotulopex
- 18th March 2007, 12:58
As often, I can find interresting information in the Basic Stamp's documentation.

At least, at my level of comprehension, it is much more clear than the PBP Compiler's manual.

Is someone else has (or had) the same problem as me, here is an extract of PARALLAX's BS2 documentation:

RANDOM generates pseudo-random numbers ranging from 0 to 65535.
They’re called “pseudo-random” because they appear random, but are
generated by a logic operation that uses the initial value in Variable to "tap"
into a sequence of 65535 essentially random numbers. If the same initial
value, called the "seed", is always used, then the same sequence of
numbers is generated.

In applications requiring more apparent randomness, it's necessary to
"seed" RANDOM with a more random value every time. For instance, in
the demo program below, RANDOM is executed continuously (using the
previous resulting number as the next seed value) while the program
waits for the user to press a button. Since the user can’t control the timing
of button presses very accurately, the results approach true randomness.

Main:
FOR trials = 1 TO 100 ' flip coin 100 times
Hold:
RANDOM flip ' randomize while waiting
BUTTON Btin, 0, 250, 100, btnWrk, 0, Hold ' wait for button press
IF (coin = 0) THEN ' 0 = heads, 1 = tails
DEBUG CR, "Heads!"
heads = heads + 1 ' increment heads counter
ELSE
DEBUG CR, "Tails..."
tails = tails + 1 ' increment tails counter
ENDIF
NEXT

PARALLAX "BASIC Stamp Syntax and Reference Manual Version 2.1

NB: Forum Administrators - please let me know if reproducing this kind of information is not allowed.