Grdmath NRAND not being random enough?

I generate two supposedly random data sets with grdmath. As long as two grdmath calls are separated by a call to another program everything seems fine (panels a) – c) in image below). But grdmath NRAND gives exact the same values if it is called in direct succession (panels d) – f) in image below) or the calls are combined into a single call (panels g) – i) in image below)

After spending way to much time staring at patterns emerging in grdmath NRAND data over in the other thread I don’t know what’s going on anymore. Maybe I just don’t see the woods for the trees. Am I doing something wrong here?

Using GMT version: 6.2.0_79f0ee5_2021.02.07

Code to play with:

#!/usr/bin/env bash


# # # # # # # # #
#               #
#   FIRST ROW   #
#               #
# # # # # # # # #

# grdmath calls seperated by calls to other programs

# First Set

gmt grdmath -R0/10/0/10 -I1 \
  0 0.5 NRAND X ADD = xa.grd \
  0 0.5 NRAND Y ADD = ya.grd

gmt grd2xyz xa.grd -o2 > x0.txt
gmt grd2xyz ya.grd -o2 > y0.txt

gmt convert -A x0.txt y0.txt > xy0.txt


# Second Set

gmt grdmath -R0/10/0/10 -I1 \
  0 0.5 NRAND X ADD = xA.grd \
  0 0.5 NRAND Y ADD = yA.grd

gmt grd2xyz xA.grd -o2 > x1.txt
gmt grd2xyz yA.grd -o2 > y1.txt

gmt convert -A x1.txt y1.txt > xy1.txt


# # # # # # # # # #
#                 #
#   SECOND  ROW   #
#                 #
# # # # # # # # # #

# grdmath calls following directly onto each other

gmt grdmath -R0/10/0/10 -I1 \
  0 0.5 NRAND X ADD = xb.grd \
  0 0.5 NRAND Y ADD = yb.grd

gmt grdmath -R0/10/0/10 -I1 \
  0 0.5 NRAND X ADD = xB.grd \
  0 0.5 NRAND Y ADD = yB.grd

gmt grd2xyz xb.grd -o2 > x10.txt
gmt grd2xyz yb.grd -o2 > y10.txt

gmt convert -A x10.txt y10.txt > xy10.txt

gmt grd2xyz xB.grd -o2 > x11.txt
gmt grd2xyz yB.grd -o2 > y11.txt

gmt convert -A x11.txt y11.txt > xy11.txt


# # # # # # # # #
#               #
#   THIRD ROW   #
#               #
# # # # # # # # #

# grdmath calls combined into single call

gmt grdmath -R0/10/0/10 -I1 \
  0 0.5 NRAND X ADD = xc.grd \
  0 0.5 NRAND Y ADD = yc.grd \
  0 0.5 NRAND X ADD = xC.grd \
  0 0.5 NRAND Y ADD = yC.grd

gmt grd2xyz xc.grd -o2 > x20.txt
gmt grd2xyz yc.grd -o2 > y20.txt

gmt convert -A x20.txt y20.txt > xy20.txt

gmt grd2xyz xC.grd -o2 > x21.txt
gmt grd2xyz yC.grd -o2 > y21.txt

gmt convert -A x21.txt y21.txt > xy21.txt


# # # # # # # # #
#               #
#   PLOTTING    #
#               #
# # # # # # # # #

gmt begin not_really_random png
  gmt subplot begin 3x3 -M10p/40p -A -Fs10c -JX10c -R0/10/0/10
    # FIRST ROW
    gmt subplot set
      gmt plot xy0.txt -Sx0.25c -W1p,red -B -B+t"xy0"
    gmt subplot set
      gmt plot xy1.txt -S+0.25c -W1p,blue -B -B+t"xy1"
    gmt subplot set
      gmt plot xy0.txt -Sx0.25c -W1p,red
      gmt plot xy1.txt -S+0.25c -W1p,blue -B -B+t"xy0 + xy1"
    # SECOND ROW
    gmt subplot set
      gmt plot xy10.txt -Sx0.25c -W1p,red -B -B+t"xy10"
    gmt subplot set
      gmt plot xy11.txt -S+0.25c -W1p,blue -B -B+t"xy11"
    gmt subplot set
      gmt plot xy10.txt -Sx0.25c -W1p,red
      gmt plot xy11.txt -S+0.25c -W1p,blue -B -B+t"xy10 + xy11"
    # THIRD ROW
    gmt subplot set
      gmt plot xy20.txt -Sx0.25c -W1p,red -B -B+t"xy20"
    gmt subplot set
      gmt plot xy21.txt -S+0.25c -W1p,blue -B -B+t"xy21"
    gmt subplot set
      gmt plot xy20.txt -Sx0.25c -W1p,red
      gmt plot xy21.txt -S+0.25c -W1p,blue -B -B+t"xy20 + xy21"
  gmt subplot end
gmt end show

Random numbers have a seed generator after which the numbers are generated. That seed often is taken from the clock. I also don’t understand why consecutive commands generate the same distribution. Time has elapsed between calls so the seed should be different. Try inserting pause(1) (or whatever that is achieved in bash) between the commands.

I tried sleep 1 but it didn’t help. I think I found the culprit: GMT appears to be case in-sensitive:

xb.grd == xB.grd

When I gave the second grid a different name it worked without problems. I would consider this a bug. What do you think @Joaquim?

Unless you are on a Mac (or secretly on Windows) xb.grd != xB.grd. This is an OS issue, not GMT’s.

1 Like

Ok, I learnt something new today. Maybe this will help somebody with a similar problem. @Joaquim is indeed right:

APFS, the file system used by MacOS, is case insensitive but case preserving. You can’t have two files named a.txt and A.txt in the same directory. For the file system those files are identical. It does however preserve which letters were uppercase and which letters were lowercase. This behaviour is different to most other file systems out there which are case sensitive.

So GMT is working perfectly; it’s a file system thing. Be careful what you name your temporary files. Thank you Joaquim for pointing this out.

You can choose to make macOS case-insensitive: https://www.macworld.com/article/3440258/how-to-convert-a-case-sensitive-mac-hfs-partition-into-a-case-insensitive-one.html