Rnd6 for Windows

Windows specific questions.
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

There is a fundamental problem with Rnd6. It only works with Windows.

What I know about Linux can be put on the back of a postage stamp. The note in FB's Help on FB_RND_ (5) mentions /dev/urandom with Linux, which only returns a single value.

So I asked ChatGPT if Linux had an equivalent to BCryptGenRandom, and it came up with:

Code: Select all

include <sys/random.h>
unsigned char buffer[32];
ssize_t result = getrandom(buffer, sizeof(buffer), 0);
if (result != sizeof(buffer)) {
    // handle error
}
Rnd6 is not a toy - it beats the pants off all of FB's generators. I reckon a Linux version would be a welcome addition.

Any takers?

:)
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

To make Rnd6 a little more readable, I have been using this:

Code: Select all

#define GetRandom BCryptGenRandom(hRand, Cast(Puchar, @Buffer(0)), 4*BufferSize, 0)
Could something along the following lines work:

Code: Select all

#ifdef __FB_LINUX__
  #define GetRandom getrandom( Cast(Puchar, @Buffer(0)), 4*BufferSize, 0 )
#else
  #define GetRandom BCryptGenRandom(hRand, Cast(Puchar, @Buffer(0)), 4*BufferSize, 0)
#endif
or am I showing my Linux ignorance?

If that is doable, then we would have a Linux compatible Rnd6. :)

At the head of Rnd6 we have:

Code: Select all

#Include Once "windows.bi"
#Inclib "bcrypt"
#Include Once "win/wincrypt.bi"
I can see that causing a few headaches, but then Linux users are used to such issues I assume. What do I know!
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

Come on, you Linux users. Is it possible to have a Linux version of Rnd6 or not?

FB_RND_REAL (5) is cross-platform.

Of course, Linux users may never visit this forum. I never visit the Linux forum. I should - I could find stuff that I could use on Windows. :)
angros47
Posts: 2397
Joined: Jun 21, 2005 19:04

Re: Rnd6 for Windows

Post by angros47 »

Using "getrandom" won't help. Its purpose is to obtain a series of random values and put them in a buffer, not to use a specific algorithm. According to the manual:

https://man7.org/linux/man-pages/man2/getrandom.2.html
By default, getrandom() draws entropy from the urandom source (i.e., the same source as the /dev/urandom device).
So, it would work exactly like FB_RND_ (5)
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

angros47 wrote:Its purpose is to obtain a series of random values and put them in a buffer, not to use a specific algorithm.
Which is exactly what I want.

Of course, 'getrandom()' may not be able to return 1024*1024 bytes so we would need to reduce BufferSize.
So, it would work exactly like FB_RND_ (5)
That contradicts your quote above.
angros47
Posts: 2397
Joined: Jun 21, 2005 19:04

Re: Rnd6 for Windows

Post by angros47 »

Sorry, I thought your main focus was to use a specific algorithm to get random numbers
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

No, I wanted to use a Linux equivalent of Microsoft's BCryptGenRandom.
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

Until recently, the last post on this thread was by dodicat on Nov 02, 2024.

On Apr 25, 2025 I came back to introduce Rnd7, a replacement for Rnd6Repeat, which, to my mind, did a better job. That was 15 days ago. I don't know what the views count was at Apr 25, 2025, but there have been a lot of views since

Rnd7 was further improved, and both adeyblue and dodicat contributed with some helpful posts.

I should be more than happy to get contributions from two of the best coders on this forum - but I am not.

The number of views currently stands at 18,139, but I have no idea if anyone is using Rnd6 or not. I am not alone - quite a few contributors never know whether their code has been used or not. We are not freeware authors - we are members of this forum and should not be treated as freeware authors. Having said that, some members have written "Thanks for sharing" but they are very few and far between. Believe it or not, I have actually written to freeware authors and thanked them for their applications. One guy was taken aback, and I got the impression I made his day, which was my intention. Have any of you ever done that?

I also have no idea if anyone downloaded the Help file. I intended to rewrite that since Rnd7 was improved. However, I have lost the will to do it.

coderJeff and fxm don't seem to get demotivated by a lack of feedback as I do, so maybe I am an oddball. Since joining this forum I have seen quite a few regular contributors cease to contribute so, perhaps, I am not such an oddball after all. I haven't said that I'll cease to contribute, but I am getting close. They ceased, but never explained why. I am having a rant, so that makes me an oddball in that respect. I have always had 'a bit of a mouth' on me, so it isn't odd for me.

How many of you have the guts to stand next to me and say: "You're right - this forum is its own worst enemy."

Of course, some of the ones who would have said that have now gone, but some may say that and still be here without contributing any more or much less than they used to. Some may say that and still be contributing regularly. Many will say nothing even if they agree with me; the lions share, I should imagine. :)
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

adeyblue
Posts: 353
Joined: Nov 07, 2019 20:08

Re: Rnd6 for Windows

Post by adeyblue »

deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

@adeyblue

Your musical taste leaves a lot to be desired.

Added: I expected a response from you, but more along the lines of throwing a grenade at me. :)
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Rnd6 for Windows

Post by deltarho[1859] »

I'm posting the latest version of Rnd6 because I said I would. I won't be updating the Help file. Only the Rnd7 topic needed a rewrite, but the Rnd7 procedure is fairly straight forward.

I tried to get Linux users to consider a Linux version of Rnd6, but that landed on deaf ears. It would have been nice to have a Linux version, in my opinion, but I won't lose any sleep with the lack of interest, as I don't use Linux.
How many of you have the guts to stand next to me and say: "You're right - this forum is its own worst enemy."
That went down like a lead balloon. :)

I'm out of this thread now.

Rnd6

Code: Select all

#Include Once "windows.bi"
#Inclib "bcrypt"
#Include Once "win/wincrypt.bi"

#define BufferSize 1024*1024
#undef Rnd
#define Rnd Rnd6
#define Range6 Rnd6Range
#define Range7 Rnd7Range
#define GetRandom BCryptGenRandom(hRand, Cast(Puchar, @Buffer(0)), 4*BufferSize, 0)
#macro Header
  Static As Uint32 count
  Static As Uint32 Buffer(BufferSize-1)
#endmacro

Namespace Globals
  Dim As BCRYPT_ALG_HANDLE ptr hRand
  Dim As Uint32 CounterIndex01, CounterIndexI, CounterIndexF
  Dim As Int32 Seed01, SeedI, SeedF
End Namespace : Using Globals

Private Function Rnd6 As Double ' unseeded
  Header
  if count = 0 then GetRandom
  function = Buffer(count)/2^32
  count += 1 : if count = BufferSize then count = 0
End Function

Private Function Rnd7Count01 As Uint32
   Function = CounterIndex01
End Function

Private Function Rnd7 As Double ' Rnd6 with seeding
  Header
  If count = 0 Or Seed01 = -1 Then GetRandom
  If Seed01 = -1 Then ' ie Buffer refresh
    Seed01 = 0 : count = 0
  Else
    count = Seed01
  End If
  Seed01+=1:If Seed01 >= BufferSize Then Seed01 = 1
  Function = Buffer(Seed01)/2^32
  count += 1:If count = BufferSize Then count = 0 
  CounterIndex01 = count
End Function

' Int32 range (unseeded)
Private Function Rnd6range Overload( First As Int32, Last As Int32 ) As Int32
  Header
  If First > Last then swap First, Last
  if count = 0 then GetRandom
  function = CLng( Buffer(count) Mod (Last-First+1)) + First ' Mod by dodicat
  count += 1 : if count = BufferSize then count = 0
End Function

Private Function Rnd7CountI As Uint32
   Function = CounterIndexI
End Function

' Int32 range (seeded)
Private Function Rnd7range Overload( First As Int32, Last As Int32 ) As Int32
  Header
  If First > Last Then Swap First, Last
  If count = 0 Or SeedI = -1 Then GetRandom
  If SeedI = -1 Then ' ie Buffer refresh
    SeedI = 0 : count = 0
  Else
    count = SeedI
  End If
  SeedI+=1:If SeedI >= BufferSize Then SeedI = 1
  Function = Clng( Buffer(SeedI) Mod (Last-First+1)) + First ' Mod by dodicat
  count += 1 : If count = BufferSize Then count = 1
  CounterIndexI = count
End Function

' Floating point range (unseeded)
Private Function Rnd6range Overload( First As Double, Last As Double ) As Double
  Header
  If First > Last Then Swap First, Last
  if count = 0 then GetRandom
  function = Buffer(count)/2^32 * ( Last - First ) + First 
  count += 1 : if count = BufferSize then count = 0
End Function

Private Function Rnd7CountF As Uint32
   Function = CounterIndexF
End Function

' Floating point range (seeded)
Private Function Rnd7range Overload( First As Double, Last As Double ) As Double
  Header
  If First > Last Then Swap First, Last
  If count = 0 Or SeedF = -1 Then GetRandom
  If SeedF = -1 Then ' ie Buffer refresh
    SeedF = 0 : count = 0
  Else
    count = SeedF
  End If
  SeedF+=1:If SeedF >= BufferSize Then SeedF = 1
  Function = Buffer(SeedF)/2^32 * ( Last - First ) + First 
  count += 1 : If count = BufferSize Then count = 1
  CounterIndexF = count
End Function

Sub on_init( ) constructor
  BCryptOpenAlgorithmProvider(@hRand, BCRYPT_RNG_ALGORITHM, 0, 0)
End Sub

Sub on_exit( ) destructor
  If hRand Then BCryptCloseAlgorithmProvider(hRand, 0)
End Sub
Post Reply