Strange Ulongint behaviour

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

Strange Ulongint behaviour

Postby deltarho[1859] » Sep 14, 2018 16:36

There is probably a simple explanation for this but I am having one of those 'cannot see the wood for the trees' moments.

Why does the results of the following seem to always be of the form nnnnnnnn00000000 ie ending in 00000000

Code: Select all

Dim As Ulong i
Randomize , 5
For i = 1 To 10
  Print Hex(Cast(Ulongint, Rnd*2^64))
  Print Hex(Culngint(Rnd*2^64))
  Print
Next
Sleep
paul doe
Posts: 701
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Strange Ulongint behaviour

Postby paul doe » Sep 14, 2018 16:55

deltarho[1859] wrote:Why does the results of the following seem to always be of the form nnnnnnnn00000000 ie ending in 00000000

Because FB's rnd() is 32-bit, and the cast simply zero-extends the remaining 32 bits. If you question was why it's casted to the high double word instead of on the low (as it seems more logical, to me at least), then I'm afraid I have no idea =D
deltarho[1859]
Posts: 1440
Joined: Jan 02, 2017 0:34
Location: UK

Re: Strange Ulongint behaviour (Solved)

Postby deltarho[1859] » Sep 14, 2018 17:12

paul doe wrote:Because FB's rnd() is 32-bit

Of course. <laugh>

I have a function which churns out 64-bit random numbers given two random Ulongs. Perhaps that is why I wrote it. However, I wrote in within the last few months so I wouldn't remember why - senile decay. <smile.

Return to “General”

Who is online

Users browsing this forum: fxm and 4 guests