starting the randomize generator with a start value?

If doesn't fit into any other category ....
Post Reply
User avatar
PANNO
Posts: 114
Joined: Thu Feb 25, 2021 4:03 am
Has thanked: 121 times
Been thanked: 25 times

starting the randomize generator with a start value?

Post 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
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: starting the randomize generator with a start value?

Post 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.
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: starting the randomize generator with a start value?

Post 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
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: starting the randomize generator with a start value?

Post 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.
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: starting the randomize generator with a start value?

Post 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
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: starting the randomize generator with a start value?

Post 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.
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: starting the randomize generator with a start value?

Post 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.
Post Reply