Recent

Author Topic: Random values of Extended type  (Read 3540 times)

fiscraliger

  • New member
  • *
  • Posts: 8
Random values of Extended type
« on: April 06, 2024, 08:15:28 pm »
Hi all,

I am searching for a code generating uniformly random floating point values of type Extended in two ranges: 0,1 and -1,1 in full Extended precision.

I already have good PRNG producing uniform 64-bit signed integers, what is missing is the formula to somehow transform these integers into random Extended in mentioned ranges. Any suggestions?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Random values of Extended type
« Reply #1 on: April 06, 2024, 08:59:41 pm »
I am unsure about your requirement but this works always for me, no matter about type, call Randomize before working with this simple method.
Code: Pascal  [Select][+][-]
  1. function RandomRangeExtended(const AMin, AMax: Extended): Extended;
  2. begin
  3.   Result := AMin + Random * (AMax - AMin);
  4. end;
Tested with input from -1 up to 1, resulted in a float between.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

wp

  • Hero Member
  • *****
  • Posts: 12067
Re: Random values of Extended type
« Reply #2 on: April 06, 2024, 09:45:07 pm »
Tested with input from -1 up to 1 ...
You probably mean "Tested with input between 0 and 1": when input is 0, the output is AMin + 0*(AMax - AMin) = AMin, and when input is 1, the output is AMin + 1*(AMax-AMin) = AMin + AMax - AMin = AMax.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Random values of Extended type
« Reply #3 on: April 06, 2024, 09:49:20 pm »
I mean when AMin is -1.1 and AMax is 1.1 (or any other two values) than you get something in that range, it could be -1.099999 or 1.099999 etc... or we not understand each other lol
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

wp

  • Hero Member
  • *****
  • Posts: 12067
Re: Random values of Extended type
« Reply #4 on: April 06, 2024, 10:39:28 pm »
Words... For me, "input" was the random number which was transformed, for you it is AMin and AMax. Alright.

fiscraliger

  • New member
  • *
  • Posts: 8
Re: Random values of Extended type
« Reply #5 on: April 06, 2024, 11:54:05 pm »
I am unsure about your requirement but this works always for me, no matter about type, call Randomize before working with this simple method.
Code: Pascal  [Select][+][-]
  1. function RandomRangeExtended(const AMin, AMax: Extended): Extended;
  2. begin
  3.   Result := AMin + Random * (AMax - AMin);
  4. end;
Tested with input from -1 up to 1, resulted in a float between.

In my case I have no such thing as "Random" to use. I have 64-bit signed integer from custom PRNG and I need to use it somehow to produce random Extended float in ranges -1,1 or 0,1.

TRon

  • Hero Member
  • *****
  • Posts: 2738
Re: Random values of Extended type
« Reply #6 on: April 07, 2024, 01:09:00 am »
In my case I have no such thing as "Random" to use. I have 64-bit signed integer from custom PRNG and I need to use it somehow to produce random Extended float in ranges -1,1 or 0,1.
Can't you simply divide your prng by 1 shl 63 (unsigned) to calculate a -1..1 range ?

Thaddy

  • Hero Member
  • *****
  • Posts: 14741
  • Censorship about opinions does not belong here.
Re: Random values of Extended type
« Reply #7 on: April 07, 2024, 07:35:45 am »
First define extended. On some 64 bit systems extended is aliased to a double, but you may want a true 80 bit extended. in that case you miss 16 bits of information to cover randoms over the full 80 bit range. Depending on application this may or may not be an issue.On linux you can simply read 80 bits from hwrand, random or urand to obtain the 80 bits needed and then transform to extended. That will give you the full range.
« Last Edit: April 07, 2024, 08:51:10 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

jamie

  • Hero Member
  • *****
  • Posts: 6290
Re: Random values of Extended type
« Reply #8 on: April 07, 2024, 04:07:20 pm »
Ok, the "," through me off !

I guess that is your use of a "." ?

In any case, you can * 10 and create a function to insert the "," after your DIV 10

or do as I do and use Currency which has leading 0's and uses  a Int64 type in the background.
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 2738
Re: Random values of Extended type
« Reply #9 on: April 07, 2024, 05:49:00 pm »
Depending on application this may or may not be an issue.
TS mentions wanting to have its "randomness" rely on a 64-bit prng so that can only lead to the conclusion that TS does not mind having lost the precision.

The part of TS statement where it reads...
Quote
in full Extended precision
...is bogus based on the first condition.

Ok, the "," through me off !
Yes, me as well but two ranges is two ranges. So it is still two ranges, 0..1 or/and -1..1.

More important to know and what TS failed to mention is the range in which the used prng create its random numbers (we can only assume the full 64 bit signed integer range). Based on that the precision gets an even further hit.

jamie

  • Hero Member
  • *****
  • Posts: 6290
Re: Random values of Extended type
« Reply #10 on: April 07, 2024, 06:24:51 pm »
Looks like a nice home work project  :o

1/Int64;  :D

What ever.
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 2738
Re: Random values of Extended type
« Reply #11 on: April 07, 2024, 06:43:27 pm »
1/Int64;  :D
If the precision does not matter than yes something to that extend  ;D

Quote
What ever.
Whenever  :D

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Random values of Extended type
« Reply #12 on: April 07, 2024, 09:10:56 pm »
I assume he wants to mirror his Int64 scaled on a -1.1 up to 0.1 extended float type.
Where -1.1 represents Low(Int64) and 0.1 represents High(Int64).
If it is like I say then the range troubles a lot, its really tight for such a big range of possibilities and second the negative/positive thingy...
In a simplified version that works with original values to compute the result, the result is sadly same if input-int64 has no significant changes.
Because of that I tried to upscale the Int64 into a QWord to do the same math, currently I do have a working version but still got bug inside.
In my case the bug is that the result is always negative, on the good side, no computed value appear twice.
I need to rethink my concept about how to scale it correctly but one question remains, what are you doing afterwards with that scaled value?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

TRon

  • Hero Member
  • *****
  • Posts: 2738
Re: Random values of Extended type
« Reply #13 on: April 07, 2024, 10:40:06 pm »
I assume he wants to mirror his Int64 scaled on a -1.1 up to 0.1 extended float type.
In case it was not clear (yet), I interpreted it differently  :-X

But it does not matter much whatever interpretation as there is simply not enough information to be able to provide the correct answer.

Does the prng create int64's in the full 64-bit range or only positive numbers, does TS require a particular scale to distribute positive and negative numbers evenly. And also the matter of precision.

Remapping is not really an issue though my quick approach gets into trouble after the 20th digit.

Quote
I need to rethink my concept about how to scale it correctly but one question remains, what are you doing afterwards with that scaled value?
If answering that question provides more answer to the already unknown variables that TS left out then yes please answer :)

fiscraliger

  • New member
  • *
  • Posts: 8
Re: Random values of Extended type
« Reply #14 on: April 08, 2024, 11:41:51 am »
In my case I have no such thing as "Random" to use. I have 64-bit signed integer from custom PRNG and I need to use it somehow to produce random Extended float in ranges -1,1 or 0,1.
Can't you simply divide your prng by 1 shl 63 (unsigned) to calculate a -1..1 range ?

I did this:


Code: Pascal  [Select][+][-]
  1. MaxInt64 := Power(2, 63);
  2.  
  3. function Random_extended:extended;
  4. Var Numerator:Int64;
  5. Begin
  6. Numerator := MyPRNG_SignedRandom64;
  7. Result := Numerator / MaxInt64;
  8. End;
  9.  
  10. function Random_extended_positive_only:extended;
  11. Var Numerator:Int64;
  12. Begin
  13. Numerator := MyPRNG_SignedRandom64;
  14. Result := Abs(Numerator / MaxInt64);
  15. End;
  16.  


Previously I tried to fiddle with internal structure of Extended, first injecting random bits into it and then trying to "temper" mantissa and exponent separately, before passing the whole record as Extended, but I failed to correctly tune these elements, so there was plenty of errors.



 

TinyPortal © 2005-2018