Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Provided automaticGlobalSeed as C-function
  • Loading branch information
MartinOtter committed Dec 10, 2015
1 parent 8f745e1 commit 5ea42e7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
within Modelica_Noise.Math.Random.Utilities;
function automaticGlobalSeed
"Creates an automatic integer seed from the current time and process id (= impure function)"
"Creates an automatic integer seed (typically from the current time and process id; this is an impure function)"
output Integer seed "Automatically generated seed";
protected
/*
protected
Integer ms,sec,min,hour "Current system time";
Integer pid "Current process ID";
algorithm
algorithm
(ms,sec,min,hour) := Modelica_Noise.Utilities.System.getTime();
pid := Modelica_Noise.Utilities.System.getPid();
seed := 1 + ms + 1000*sec + 1000*60*min + 1000*60*60*hour
+ 6007*pid;
+ 6007*pid;
*/
external "C" seed = ModelicaRandom_automaticGlobalSeed()
annotation (Include = "#include \"ModelicaRandom.c\"");

annotation (Documentation(info="<html>
<h4>Syntax</h4>
Expand All @@ -18,17 +22,14 @@ seed = Utilities.<b>automaticGlobalSeed</b>();
</pre></blockquote>
<h4>Description</h4>
<p>Returns an automatically computed seed (Integer) from:</p>
<p>Returns an automatically computed seed (Integer). Typically, this seed is computed from:</p>
<ol>
<li> The current localtime by computing the number of milli-seconds up to the current hour</li>
<li> The process id (added to the first part by multiplying it with the prime number 6007).</li>
</ol>
<p>Check that worst case combination can be included in an Integer:</p>
<blockquote>
<p>1000*60*60 = 3.6e6 &LT; 2^31 = 2147483648 (2.1e9)</p>
</blockquote>
<p>
Everything is added to 1, in order to guard against the very unlikely case that the sum is zero.
If getTime and getPid functions are not available on the target where this Modelica function
is called, other means to compute a seed may be used.
</p>
<p>
Expand Down
28 changes: 25 additions & 3 deletions Modelica_Noise 1.0 Beta.1/Resources/Include/ModelicaRandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
Implemented a first version.
This file is licensed under the BSD 2-Clause License:
Copyright (C) 2015, DLR and Modelica Association.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Expand Down Expand Up @@ -588,6 +588,27 @@ MODELICA_EXPORT double ModelicaRandom_impureRandom_xorshift1024star(int id) {



MODELICA_EXPORT int ModelicaRandom_automaticGlobalSeed() {
/* Creates an automatic integer seed (typically from the current time and process id) */


int ms, sec, min, hour, mday, mon, year;
int pid;
int seed;

ModelicaRandom_getTime(&ms, &sec, &min, &hour, &mday, &mon, &year);
pid = ModelicaRandom_getpid();

/* Check that worst case combination can be included in an Integer:
1000*60*60 = 3.6e6 < 2^31 = 2147483648 (2.1e9)
Everything is added to 1, in order to guard against the very unlikely case that the sum is zero.
*/
seed = 1 + ms + 1000*sec + 1000*60*min + 1000*60*60*hour + 6007*pid;
return seed;
}


/* original algorithms */

Expand All @@ -605,4 +626,5 @@ MODELICA_EXPORT void ModelicaRandom_convertRealToIntegers(double d, int i[]) {




#endif

0 comments on commit 5ea42e7

Please sign in to comment.