Better way for leading zeros?

I feel there must be a better way to achieve this. As best I can determine, I can’t use the ToBinary max digits option because my numbers are too big (48 bits). So I’ve resorted to this kludgy string fix.

Any ideas?

Also, I couldn’t find a command in Xojo equivalent to an old BASIC concept of printing multiple characters such as binary$ = CHAR(“0”, 48). Anything out there like this?

Many thanks!

var b as string
var l as integer
l = 48 // Number of binary digits
for n as integer = 1 to 10 // Print 10 examples
  b = Bin(system.random.inrange(0,2^l-1)) // Generate a random number and convert to binary
  // Make sure leading zeros are prepended to ensure total number of binary digits.
  if len(b)<l then
    b = left("000000000000000000000000000000000000000000000000",l-len(b)) + b
  End
  system.debuglog(b)
next

Var b As Uint64 = &b110100011001000110010001100100011001000110010001
msgbox b.ToBinary(48)

Thank you Rick.

I was able to adapt your Uint64 solution, but in the process, I ended up not having to use the Uint64 type, but I swear I tried this (below) before and it didn’t work. Now it does. By simply adding the .toBinary function to the random generation code, it all works now. I must have goofed the b variable type the first time.

However, regardless of whether I use the method below or the Uint64 approach, the binary values are only accurate up to 57 bits. Any clue what’s going on there? Running the below code to produce 64 bit binary numbers produces 64 bits, but the least significant are all zeros.

var b as string
var n, l as integer
l = 64
for n = 1 to 10
  b = system.random.inrange(0,2^l-1).tobinary(l)
  textarea1.text = textarea1.text + b + EndOfLine
next n

Many thanks!

Correction, after further analysis, it seems the converted numbers are accurate up to 64 bits. But apparently, the randomly generated numbers are all strangely of values no granular than about 10 bits. I mean, I’m a newbie, but what the…?

Something related to the internal of random numbers. For example, the Rnd() function returns a Double >= 0 and < 1, but such floating point has accuracy around 16 digits or less, and max uint64 has 20 decimal digits, I think that the internal math for InRange() probably is crossing some limitations causing some truncation.

// Lets make a pseudorandom 64 bit number generator using 2 blocks of 32

Public Function Random64() As Uint64
  
  Var m As New MemoryBlock(8) // 64 bits
  m.Int32Value(0) = System.Random.InRange(-1, &h7fffffff)
  m.Int32Value(4) = System.Random.InRange(-1, &h7fffffff)
  Return m.UInt64Value(0)
  
End Function

// Show some samples

For i As Integer = 0 to 9
  System.DebugLog Random64.ToBinary(64)
Next

Ah, you took me to a place I haven’t been yet. I had read up on MemoryBlock, but had not yet jumped in on that one. Thank you for that.

I am curious about your choice of parameters (-1, &h7fffffff). I would have chosen (0, 2^32-1). Any reason? It seems your choice would generate 63 bit numbers at best. But admittedly, I am a newbie, so apologies.

  • Scott :flushed:

I was wrong with this one, it should be from -2147483648, to &h7fffffff, sorry.

Because the manual says lower and higher values of an integer (signed) range, and those should be the proper ones for 32 bit.

So…

// Lets make a pseudorandom 64 bit number generator using 2 blocks of 32

Public Function Random64() As Uint64
  
  Var m As New MemoryBlock(8) // 64 bits
  m.Int32Value(0) = System.Random.InRange(-2147483648, &h7fffffff)
  m.Int32Value(4) = System.Random.InRange(-2147483648, &h7fffffff)
  Return m.UInt64Value(0)
  
End Function

// Show some samples

For i As Integer = 0 to 9
  System.DebugLog Random64.ToBinary(64)
Next