Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a process sleep for a random time - Erlang

Tags:

erlang

I would like to use the sleep function of the timer class and I would like to use it to make a process sleep for a random amount of time in between 0 and 1.

In order to do this, I tried:

timer:sleep(random:uniform()).

However, i get the following error message:

=ERROR REPORT==== 1-Apr-2014::21:39:32 ===
Error in process <0.472.0> with exit value: {timeout_value,[{timer,sleep,1,       
[{file,"timer.erl"},{line,152}]},{myModule,myFunction,4,[{file,"myModule.erl"},
{line,46}]}]}

What is wrong with this?

Also, as I have multiple versions of the same process, how can I make sure that each start with a different random number?

like image 520
Haych Avatar asked Mar 20 '23 00:03

Haych


1 Answers

{A1,A2,A3} = now(),
random:seed(A1, A2, A3), 

You can run this code in every process start function. Because the randon seed is stored in process dictionary, every process has different process dictionary. you'd better set random seed for every process.

Then you case use uniform() -> float() to get an random number between 0.0 and 1.0, use seconds(Seconds) -> MilliSeconds to get the number of milliseconds in Seconds. call sleep(Time) -> ok to suspend the process calling this function for Time amount of milliseconds.

like image 172
BlackMamba Avatar answered Mar 28 '23 02:03

BlackMamba