Page 1 of 1

starting the randomize generator with a start value?

Posted: Thu May 13, 2021 9:08 pm
by PANNO
is it possible to start the random generator with a startvalue so we can have the same rnd () on all devices ?

randomize (1999)
wlog rnd(100) - same bytesequenz on every devices .

thx

Re: starting the randomize generator with a start value?

Posted: Wed Nov 02, 2022 1:45 pm
by BeanieBots
I too required this but could not find a way of doing it in Annex, so I wrote my own.

Code: [Local Link Removed for Guests]


Seed = 12345678
For N = 1 to 20
  Gosub Random
  Dice =  Int(6*Rand+1)
  wlog Rand,Dice
Next N
wait

Random:
  Rand = (4.3*Seed+3.3e9) mod 1e8
  Seed = Rand
  Rand=Rand/1e8
Return
It produces a reasonably random 8 digit number between 0 and 1
The seed can be any 8 digit number to produce different sequences.
EDIT: Changed a few numbers to give a better sequence.

Re: starting the randomize generator with a start value?

Posted: Wed Nov 02, 2022 4:14 pm
by cicciocb
HI all,
looks this is an old post that I missed.
In fact I just re-discovered (I forgot to document) that it is possible to define the seed simply giving an initial negative number as argument of RND.

You can find a live example here

Re: starting the randomize generator with a start value?

Posted: Sat Nov 05, 2022 4:08 pm
by BeanieBots
rnd function not working for me with Annex32 BLE CAN 1.47.2 (at least, not as expected).
rnd(N) only returns a random float when N=1
Any -ve number returns only 0
Any number >1 only returns a random INTEGER between 0 and the value used.
Just tested with 1.44.2 with same results.
Also tried 1.44.2 on ESP2866. Same results.
Tried changing the values in the simulator to which you linked. Results totally erratic.
Not sure what is going on.

Re: starting the randomize generator with a start value?

Posted: Sat Nov 05, 2022 4:24 pm
by cicciocb
HI Bernie,
don't know exactly what you are expecting from this function.
The doc says :
"
RND(number)
Returns a random number in the range from 0 to ‘number' -1
If ‘number’ is equal to 1, the result will be a value less than 1 but greater than or equal to zero.
"
I included this text for the next release of the help:
If ‘number’ is a negative number, it will be used as seed by the pseudo-random number generator algorithm. This will force a deterministic sequence.

The function works as expected, at least as per the documentation.
The result of the function used with a negative number has no meaning as it just set the seed for the next random sequence

Re: starting the randomize generator with a start value?

Posted: Sat Nov 05, 2022 5:53 pm
by BeanieBots
I think I have miss-understood the use of rnd(-N)
From your last reply now I understand that rnd(-N) is used only once to set the seed for the next sequence.
Then use rnd(1) to produce a random number where 0 < RND <1
Use rnd(-1) again to repeat the sequence.
However;
rnd(10) produces INTEGER numbers between 0 and 10 (0<=rnd<10)
I was expecting FLOATS. This is not a problem, just not expected.
Sorry for my missunderstanding.

Re: starting the randomize generator with a start value?

Posted: Sat Nov 05, 2022 6:54 pm
by bugs
rnd(10) produces INTEGER numbers between 0 and 10 (0<=rnd<10)
I was expecting FLOATS. This is not a problem, just not expected.
Yes - I met that earlier today and then used rnd(1) * 10 to get the required float values.