RANDOM is broken in C11.1.13815

Simple code that produces a sequence of random numbers:

  PROGRAM

  MAP
    INCLUDE('printf.inc'), ONCE
  END

i                             LONG, AUTO
v                             LONG, AUTO

  CODE
  LOOP i=1 TO 10
    v = RANDOM(0, 360)
    printd('random[%i]=%i', i, v)
  END

In C11.1.13815 it always produces the same sequence of numbers, in my case it is

random[1]=312
random[2]=62
random[3]=234
random[4]=214
random[5]=15
random[6]=26
random[7]=349
random[8]=212
random[9]=214
random[10]=241

Checked in C8 and C10 - the sequence is different every time.

Hi Mike - I wonder if srand or randomize would help.

It does seem to work, if a sucky workaround is OK.

  MAP
    MODULE('')
      SRand(ushort),name('_srand')
    END
  END
    SRand(CLOCK()+i)
    v = RANDOM(0, 360)

Interesting, Mike.
Have you sent that into SV support? Hopefully, that is something they would jump on right away to fix.
I tested with 11 13505 and it does not happen with that version…

It does happen in .13815

1 Like

There is definitely no such problem in Clarion 11.1.13845 …

2 Likes

Thanks, tt’s a good news!

An interesting aside;

StringTheory uses Random in the generation of Guids. Since random seeds from the time this worked fine.

Until one enterprising programmer had a process that started multiple child exes in the same centisecond.

So now StringTheory initialises the random number using a combination of the time, and process id. So if you run the above test after calling st.random, itll be fine even on the noted build.

1 Like

Very strange things. There is a CoCreateGuid() method for GUID in WinAPI. Why don’t we know the names of those who do all this?

Hi Genricke -
I’m not sure I understand what you are asking? Perhaps “why doesn’t StringTheory use CoCreateGuid?”
Or something else?

Bruce

Yes! Why String Theory is used to create a Random() GUID.

You might find this reference helpful in understanding this explanation; Universally unique identifier - Wikipedia

The short version is that all uuids are 128 bits long. The ones you are familiar with on MS platforms are typically displayed as hex-encoded (32 nibbles plus some hyphens).

While stringtheory has a method for generating version 4 uuids, for general use it prefers to use a ASCII value in 16 chars, which represents around 80 bits of entropy.

This is sufficient for the main use case (unique per table) and can be generated on any platform, in any language. Its also visible and editable in readers (like topscan) without encoding or decoding. It also does not need encoding in text formats like CSV, JSON or XML. It can even be used (and generated) in Excel and so on.

It’s fundamentally suitable for two use cases.

A) replacing auto-numbering (client or server side) and so bypassing all the problems that auto numbering has.

B) data distribution - allowing data to exist in multiple databases (including sql, isam, and text) at the same time.

C) compatible with all platforms and all languages. (Although generating version 4 uuids is not hard, it can be tricky because of the embedded version number.)