View Issue Details

IDProjectCategoryView StatusLast Update
0000233OpenFOAMBugpublic2011-11-03 17:12
Reporterwyldckat Assigned Touser4 
PrioritynormalSeveritytweakReproducibilityalways
Status resolvedResolutionfixed 
Summary0000233: srand48, lrand48 and drand48 are POSIX and therefore OS specific
DescriptionOpenFOAM's random functions are directly relying on POSIX (srand48, lrand48, drand48) or Unix functions (random, srandom).

Attached is a patch that extracts these "basic" functions from the OpenFOAM core to the OS specific _outlier level_.
Steps To ReproduceWhen "hacking" the code for compatibility with other Operating Systems, the Foam::Random class gets a seriously "ugly hack" done on it :(
Additional InformationThis separation can make life a bit easier for changing how the random numbers are calculated, including some optimizations or upgrades can be made for using more or less bits (GMP or MPFR? Or even independent hardware random number generator!).
TagsNo tags attached.

Activities

wyldckat

2011-06-29 11:18

updater  

user4

2011-07-01 16:16

  ~0000502

see commit

a53a51fcc0679440d6dc3ab396e3da080a179205

Thanks for that.

wyldckat

2011-07-01 22:57

updater   ~0000503

Nice! But... I think you forgot to push it to the repo 2.0.x on github :(

user4

2011-07-04 09:53

  ~0000507

I did indeed forget the push. Should be there now.

Thanks, Mattijs

wyldckat

2011-07-04 12:50

updater   ~0000513

I'm sorry for re-opening this issue again, but I forgot about the need to divide by INT_MAX when using USE_RANDOM. I only noticed it when reviewing the commit you pushed.
The attached file "patch_random_unix_part" fixes this last issue.

wyldckat

2011-07-04 12:50

updater  

patch_random_unix_part (707 bytes)   
diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C
index dba1e73..c79d8f0 100644
--- a/src/OSspecific/POSIX/POSIX.C
+++ b/src/OSspecific/POSIX/POSIX.C
@@ -59,6 +59,10 @@ Description
 
 #ifdef USE_RANDOM
 #   include <climits>
+#   if INT_MAX    != 2147483647
+#       error "INT_MAX    != 2147483647"
+#       error "The random number generator may not work!"
+#   endif
 #endif
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@@ -1250,7 +1254,7 @@ Foam::label Foam::osRandomInteger()
 Foam::scalar Foam::osRandomDouble()
 {
 #ifdef USE_RANDOM
-    return (scalar)random();
+    return (scalar)random()/INT_MAX;
 #else
     return drand48();
 #endif
patch_random_unix_part (707 bytes)   

user4

2011-07-04 14:41

  ~0000515

Fixed in commit f6fa6eb9dafe4e1eea0fbdead745ba641afae6de

wyldckat

2011-10-06 11:50

updater   ~0000690

Last edited: 2011-10-06 11:51

I missed a line in the initially proposed patch.
The recently attached file "cachedRandom_missing_patch" has the missing change.
The "#if INT_MAX then error" at the top might also be discarded, since it's not being used in this particular file.

Note: I saw this thanks to Symscape's mingw patches.

wyldckat

2011-10-06 11:51

updater  

cachedRandom_missing_patch (526 bytes)   
diff --git a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C
index 4bf4054..7cd5d84 100644
--- a/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C
+++ b/src/OpenFOAM/primitives/random/cachedRandom/cachedRandom.C
@@ -79,7 +79,7 @@ Foam::cachedRandom::cachedRandom(const label seed, const label count)
     osRandomSeed(seed_);
     forAll(samples_, i)
     {
-        samples_[i] = drand48();
+        samples_[i] = osRandomDouble();
     }
 }
 
cachedRandom_missing_patch (526 bytes)   

user4

2011-11-03 17:12

  ~0000783

pushed to 0721eb3218b63f48209b54fa13f5e23825a141ec

Thanks for cleanup.

Issue History

Date Modified Username Field Change
2011-06-29 11:18 wyldckat New Issue
2011-06-29 11:18 wyldckat File Added: patch_random_to_OSspecific.gz
2011-07-01 16:16 user4 Note Added: 0000502
2011-07-01 16:16 user4 Status new => resolved
2011-07-01 16:16 user4 Fixed in Version => 2.0.x
2011-07-01 16:16 user4 Resolution open => fixed
2011-07-01 16:16 user4 Assigned To => user4
2011-07-01 22:57 wyldckat Note Added: 0000503
2011-07-01 22:57 wyldckat Status resolved => feedback
2011-07-01 22:57 wyldckat Resolution fixed => reopened
2011-07-04 09:53 user4 Note Added: 0000507
2011-07-04 09:53 user4 Status feedback => closed
2011-07-04 09:53 user4 Resolution reopened => fixed
2011-07-04 12:50 wyldckat Note Added: 0000513
2011-07-04 12:50 wyldckat Status closed => feedback
2011-07-04 12:50 wyldckat Resolution fixed => reopened
2011-07-04 12:50 wyldckat File Added: patch_random_unix_part
2011-07-04 14:41 user4 Note Added: 0000515
2011-07-04 14:41 user4 Status feedback => resolved
2011-07-04 14:41 user4 Resolution reopened => fixed
2011-10-06 11:50 wyldckat Note Added: 0000690
2011-10-06 11:50 wyldckat Status resolved => feedback
2011-10-06 11:50 wyldckat Resolution fixed => reopened
2011-10-06 11:51 wyldckat File Added: cachedRandom_missing_patch
2011-10-06 11:51 wyldckat Note Edited: 0000690
2011-11-03 17:12 user4 Note Added: 0000783
2011-11-03 17:12 user4 Status feedback => resolved
2011-11-03 17:12 user4 Resolution reopened => fixed