Old 05-29-2023, 12:32 PM   #1
papagirafe
Human being with feelings
 
papagirafe's Avatar
 
Join Date: Aug 2020
Location: Brasil
Posts: 679
Default set seed for rand()?

Is there a way to set the seed for the rand() function so that I can have a stable sequence between video frames calculations?
papagirafe is offline   Reply With Quote
Old 05-29-2023, 01:04 PM   #2
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

No, I don't think you can, which is a shame. You could populate a lookup table with random values in @init and use that instead.

I may be wrong but I think rand() output might be related to playback position but don't quote me on that.
IXix is offline   Reply With Quote
Old 05-29-2023, 01:14 PM   #3
papagirafe
Human being with feelings
 
papagirafe's Avatar
 
Join Date: Aug 2020
Location: Brasil
Posts: 679
Default

Quote:
Originally Posted by IXix View Post
No, I don't think you can, which is a shame. You could populate a lookup table with random values in @init and use that instead.
<snip>
Good suggestion and it works! Unfortunately in VP there is no @init section so the code is not as clean as it could.
papagirafe is offline   Reply With Quote
Old 05-29-2023, 11:44 PM   #4
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,645
Default

You could code your own pseudo random generator, that way you can seed it as you please. There is an efficient MINSTD random generator in my JSFX pack, and also a 64-bit xorshift example.
Tale is offline   Reply With Quote
Old 05-30-2023, 12:34 PM   #5
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

Oh yeah, what Tale said! Roll your own is way better. Here's one from a long time ago by Liteon... https://forum.cockos.com/showpost.ph...45&postcount=2
IXix is offline   Reply With Quote
Old 05-31-2023, 10:17 AM   #6
jack461
Human being with feelings
 
jack461's Avatar
 
Join Date: Nov 2013
Location: France
Posts: 181
Default (yet another Radom Generator)

Here is one I use in a big project:

Code:
/*
      Random number generator

      Hull–Dobell Theorem :
      Linear congruential generator x <-- (x * a + c) % m is correct for:
        m and c are relatively prime,
        a − 1 is divisible by all prime factors of m,
        a − 1 is divisible by 4 if m is divisible by 4.
*/
MaxDouble = 1.7976931348623158  * 10^308;
MinDouble = -1.7976931348623158  * 10^308;
Rndm_m = 256 * 256 * 256 * 256 ; // Modulo: 2^32
Rndm_m1 = Rndm_m - 1;
Rndm_a = 4 * 8191 + 1;  // 8191 is a prime
Rndm_c = 8167; // a prime, less than 8191
Rndm_n = 59933; // the seed : ANY value will do
// next seed is then "Rndm_n = (Rndm_n * Rndm_a + Rndm_c) % Rndm_m;"

// provide a random integer in [0 .. k-1]
function irand(k)
(
    Rndm_n = (Rndm_n * Rndm_a + Rndm_c) % Rndm_m;
    floor(k * Rndm_n / Rndm_m);
);

// provide a random float in [0 .. 1]
function frand()
(
    (Rndm_n = (Rndm_n * Rndm_a + Rndm_c) % Rndm_m) / Rndm_m1;
);

// provide a random float in [a b]
function frand(a, b)
(
    a + (b - a) * (Rndm_n = (Rndm_n * Rndm_a + Rndm_c) % Rndm_m) / Rndm_m1;
);

// provide a random float between a and b according to an exponential distribution
function xrand(a,b)
(
    (a <= 0) ? a = MinDouble : a = log(a);
    (b <= 0) ? b = MinDouble : b = log(b);
    exp (a + (b - a) * (Rndm_n = (Rndm_n * Rndm_a + Rndm_c) % Rndm_m) / Rndm_m1);
);
The idea is that, whatever you use in your algorithm (irand, frand, xrand), you can run it again identically by just resetting the seed, the variable Rndm_n.

Hope that help.

J. Jack.

PS: just for the info, the algorithm referenced by https://forum.cockos.com/showpost.ph...45&postcount=2 was first devised for the implementation of the APL\360 language by Kenneth E. Iverson in the sixties, with these precise values 16807 (aka 7^5) and 2147483647 (2^31 - 1).
jack461 is offline   Reply With Quote
Old 06-02-2023, 04:48 AM   #7
papagirafe
Human being with feelings
 
papagirafe's Avatar
 
Join Date: Aug 2020
Location: Brasil
Posts: 679
Default

Quote:
Originally Posted by jack461 View Post
Here is one I use in a big project:
<snip>
The idea is that, whatever you use in your algorithm (irand, frand, xrand), you can run it again identically by just resetting the seed, the variable Rndm_n.

Hope that help.

J. Jack.

PS: just for the info, the algorithm referenced by https://forum.cockos.com/showpost.ph...45&postcount=2 was first devised for the implementation of the APL\360 language by Kenneth E. Iverson in the sixties, with these precise values 16807 (aka 7^5) and 2147483647 (2^31 - 1).
Thanks a lot for the source code!!!! That will work with minimal adjustement. I prefer your solution because it doesn't involve dealing the unfamous "virtual local address space". Just for the sake of documentation here is a barebone version of the solution I worked with:
Code:
//vp:accross frames random generator
_rndBase=0;
function init(numRnd) global(_rndBase) instance(initialized,cr,rnd) local(i)
(
  !initialized?(
    initialized=1;
    rnd=_rndBase;
    i=-1;loop(numRnd,rnd[i+=1]=rand(1)));
  cr=0;
  _rndBase+=numRnd
);
function rnd(v) instance(cr,rnd) local(r)
(
  r=(rnd[cr]*(v)+.05)|0;
  cr=(cr+1);
  r;
);
num.init(100);a=num.rnd(31);  //for a sequence of maximum 100 random numbers between 0-31
num2.init(10);b=num2.rnd(5);  //for an independant series of 10 random numbers between 0-5
*** update: I was able to substitute my rnd()/table by a one liner function based on your input!

Last edited by papagirafe; 06-02-2023 at 08:56 AM. Reason: status update
papagirafe is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 08:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.