Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

General FreeBASIC programming questions.
Post Reply
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by deltarho[1859] »

Oh dear, I did not spot that fxm was sneaking some mathematics in.

I understood every word of fxm's last post but cannot imagine why anyone would want to create a union of adjacent variation intervals.

As a side note, it seems a different syntax is being employed in France compared with the UK. I would write E[-5,0), for example, whereas fxm would write E[-5,0[. Not that it matters so long as each understands the other.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by MrSwiss »

fxm, thanks for the explanation (union operator), but who want's/need's that in practice?
I'd rather call that: joining two ranges (which gives the overlap at 0)
deltarho wrote:I understood every word of fxm's last post but cannot imagine why anyone would want to create a union of adjacent variation intervals.
The solution (practically) is to choose a range, that fit's the purpose (two/more, if needed).
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by fxm »

deltarho[1859] wrote:As a side note, it seems a different syntax is being employed in France compared with the UK. I would write E[-5,0), for example, whereas fxm would write E[-5,0[. Not that it matters so long as each understands the other.
Yes, it's French notation, sorry.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by fxm »

MrSwiss wrote:I'd rather call that: joining two ranges
https://www.probabilitycourse.com/chapt ... ations.php
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by deltarho[1859] »

Perhaps MrSwiss uses [ and not ) and I am the odd one out. As I said, it matters not if we understand each other's syntax.

I have not published my testing code either and found a silly mistake. I am no longer getting a distribution issue with FB's rnd_range.

So, this is where I am now standing:

Code: Select all

Function rnd_range Overload (first As Double, last As Double) As Double
    Function = Rnd * (last - first) + first
End Function

Function rnd_range Overload (first As Longint, last As Longint) As LongInt
  Function = Rnd*(last - first -(first>=0)) - .5 + first
End Function
I still think it worthwhile to include the integer variation into the manual because if anyone used Int with the manual's Function then they would not realize that the distribution was skewed. It would need to be thoroughly tested by more than one and definitely not take my word for it. It should also be mentioned that the Double version should be used for a continuous range and the LongInt version should be used for a discrete range.

It is not clear to me now what Function 'Banker's rounding' was referring to since I did not propose a Function for Double - I was using the Function from FB's manual.

Yes, fxm is using Set Theory although I would regard that as a branch of Pure Mathematics and not Statistics.
Last edited by deltarho[1859] on Nov 01, 2018 17:46, edited 2 times in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by MrSwiss »

delarho[] wrote:You have never published your code for testing Doubles.
No, it's the same quick & dirty hack, I've also used before, with 'int' types,
just some of them, are Doubles now:
t, ll, hl, a(ll To hl) -- and the iterator, in the output loop (added: step 0.1).

Added:
delarho[] wrote:Double version should be used for a continuous range and the LongInt version should be used for a discrete range.
There is a little problem in that, because, choosing which 'overloaded' Function
is used, is 'out of our hands', aka: the compilers choice.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by deltarho[1859] »

I have two programs: One for testing floats and the other for testing integers.

The float program was written first and the integer program was the float program but adapted. However, because "They have to be treated very differently" I should have written the integer program from scratch. The '-(first>=0)' was introduced to correct what I thought was an anomaly. I was actually correcting a bug that was not known to me at the time. Dafhi's method was correct all along!

I am reminded of Albert Einstein who got some results from an equation which he was unable to believe. To adhere to his belief he introduced what he called a cosmological constant. Sometime later Hubble, via observations, proved that Einstein's original equation was right in the first place and Einstein's mathematics predicted what nobody had dreamed of; including Einstein, of course. Einstein later referred to the cosmological constant as the biggest blunder of his life. Well, my blunder is not on the same scale but is a blunder nonetheless.

So, the LongInt function now reads:

Code: Select all

Function rnd_range Overload (first As Longint, last As Longint) As LongInt
  Function = Rnd*(last - first + 1) - .5 + first
End Function 
I would appreciate it if someone would thrash the living daylights out of that. If it stands up to serious bombardment then perhaps it may qualify for adding to the manual; with dafhi taking full credit. You may recall dafhi mentioned it because my use of Int was slow in comparison.
MrSwiss wrote:There is a little problem in that, because, choosing which 'overloaded' Function
is used, is 'out of our hands', aka: the compilers choice.
I meant with regard commenting in the manual so that folks would know which to use.
Last edited by deltarho[1859] on Nov 01, 2018 19:45, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by MrSwiss »

deltarho[1859] wrote:I meant with regard commenting in the manual so that folks would know which to use.
Me too, but to choose (for the programmer), they'd have to be named different
and, not overloaded.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by deltarho[1859] »

MrSwiss wrote:but to choose (for the programmer), they'd have to be named different and, not overloaded.
Overloaded functions are not something that I have used often but I cannot see why we should be using different names. It seems to me that all we need do is explain that for a continuous range Double should be used and for a discrete range, LongInt should be used.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by MrSwiss »

deltarho[1859] wrote:It seems to me that all we need do is explain that for a continuous range Double should be used and for a discrete range, LongInt should be used.
Which is IMO quite useless, unless the programmer 'has the choice' by choosing
a differently named Function, which is the difference to 'overload': same name
but, different data-types (compilers choice).

Short-cut version: different behaviour = different name, same behaviour = overload
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by dodicat »

Has anybody just used a ulongint generator for a range , and skipped the double (rnd* e.t.c.)?
I ask because it saves me searching.
Something like perhaps:
EDIT made longint generator, but ulongint is OK also for +ve ranges.

Code: Select all




'any ulongint generator
'Use this one here
type Rand
   a as ulongint
   b as ulongint
   c as ulongint
   d as ulongint
end type

function ranulong(x as rand) as ulongint
   dim e as ulongint = x.a - ((x.b shl 7) or (x.b shr (64 - 7)))
   x.a = x.b xor ((x.c shl 13) or (x.c shr (64 - 13)))
   x.b = x.c + ((x.d shl 37) or (x.d shr (64 - 37)))
   x.c = x.d + e
   x.d = e + x.a
   return x.d
end function

'function randouble(x as rand) as double ''NOT NEEDED
    'return ranulong(x)/18446744073709551615ull
   ' end function

 sub init(x as rand, byval seed as ulongint=100)
   dim i as ulongint
    x=type(4058668781,seed,seed,seed)
    for i as ulongint=1 to 20
        ranulong(x)
        next
end sub

'=========
dim shared as rand z
init(z)
'========


function range(f as longint,l as longint) as longint 'for any ranges, ulongint for + only ranges
    return (ranulong(z) mod ((l-f)+1)) + f
end function


dim as double t=timer
redim as long a(-2 to 2)

for n as long=1 to 10000000
    var t=range(-2,2)
    a(t)+=1
next

for n as long=lbound(a) to ubound(a)
    print n,a(n)
next
'================
print
redim a(-5 to -1)

for n as long=1 to 10000000
    var t=range(-5,-1)
    a(t)+=1
next

for n as long=lbound(a) to ubound(a)
    print n,a(n)
next

'===============
print
redim a(0 to 3)

for n as long=1 to 10000000
    var t=range(0,3)
    a(t)+=1
next

for n as long=lbound(a) to ubound(a)
    print n,a(n)
next
'================
print
redim a(13 to 14)

for n as long=1 to 10000000
    var t=range(13,14)
    a(t)+=1
next

for n as long=lbound(a) to ubound(a)
    print n,a(n)
next

print
print timer-t;"  seconds"
print "Press a key for 101 and some raw ulongs"
sleep
redim a(0 to 100)

for n as long=1 to 50000000
    var t=range(0,100)
    a(t)+=1
next

for n as long=lbound(a) to ubound(a)
    print n,a(n),ranulong(z),range(-1,1)
next
print "done"
sleep


  
Last edited by dodicat on Nov 01, 2018 21:07, edited 1 time in total.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by deltarho[1859] »

MrSwiss wrote:Which is IMO quite useless
I take the view that we have two range functions: One continuous and one discrete. They both take low and high limits so it seemed natural to overload them.
dodicat wrote:Has anybody just used a ulongint generator for a range , and skipped the double (rnd* e.t.c.)?
What do you mean by "and skipped the double (rnd* e.t.c.)"
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by dodicat »

Straight from longint or ulongint to a range without using rnd* or a custom rnd* .
Somebody must have made one along the way.
(By the way I like Dafhi's -.5 instead of int() for the float one)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by MrSwiss »

I don't think that's feasible, without a [0, 1) or [0, 1[ generated value (as multiplyer).
Except one can maybe do it, by applying a clamp, perhaps (dunno). This would make
it probably rather ineffective, as well as difficult, to get a even distribution.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Bernard Widynski's Middle Square Weyl Sequence RNG (MsWs)

Post by dodicat »

The distributions seem not too bad Mr Swiss, as shown by my arrays (here anyway).
It is also much faster than dipping into double and multiplying.
The uniformity seems OK.
Perhaps the randomness needs further testing.
Post Reply