Random values of Extended type lazarus / 21-General ---------------------------------------------------------------------- fiscraliger · 2024-04-06 18:15 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 · 2024-04-06 18:59 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function RandomRangeExtended(const AMin, AMax: Extended): Extended;begin  Result := AMin + Random * (AMax - AMin);end;Tested with input from -1 up to 1, resulted in a float between. ---------------------------------------------------------------------- wp · 2024-04-06 19:45 Quote from: KodeZwerg on April 06, 2024, 08:59:41 pmTested 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 · 2024-04-06 19:49 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 ---------------------------------------------------------------------- wp · 2024-04-06 20:39 Words... For me, "input" was the random number which was transformed, for you it is AMin and AMax. Alright. ---------------------------------------------------------------------- fiscraliger · 2024-04-06 21:54 Quote from: KodeZwerg on April 06, 2024, 08:59:41 pmI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function RandomRangeExtended(const AMin, AMax: Extended): Extended;begin  Result := AMin + Random * (AMax - AMin);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 · 2024-04-06 23:09 Quote from: fiscraliger on April 06, 2024, 11:54:05 pmIn 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 · 2024-04-07 05:35 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. ---------------------------------------------------------------------- jamie · 2024-04-07 14:07 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. ---------------------------------------------------------------------- TRon · 2024-04-07 15:49 Quote from: Thaddy on April 07, 2024, 07:35:45 amDepending 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... Quotein full Extended precision ...is bogus based on the first condition. Quote from: jamie on April 07, 2024, 04:07:20 pmOk, 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 · 2024-04-07 16:24 Looks like a nice home work project  1/Int64;  What ever. ---------------------------------------------------------------------- TRon · 2024-04-07 16:43 Quote from: jamie on April 07, 2024, 06:24:51 pm1/Int64;  If the precision does not matter than yes something to that extend  QuoteWhat ever. Whenever ---------------------------------------------------------------------- KodeZwerg · 2024-04-07 19:10 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? ---------------------------------------------------------------------- TRon · 2024-04-07 20:40 Quote from: KodeZwerg on April 07, 2024, 09:10:56 pmI 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  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. QuoteI 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 · 2024-04-08 09:41 Quote from: TRon on April 07, 2024, 01:09:00 amQuote from: fiscraliger on April 06, 2024, 11:54:05 pmIn 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MaxInt64 := Power(2, 63); function Random_extended:extended;Var Numerator:Int64;BeginNumerator := MyPRNG_SignedRandom64;Result := Numerator / MaxInt64;End; function Random_extended_positive_only:extended;Var Numerator:Int64;BeginNumerator := MyPRNG_SignedRandom64;Result := Abs(Numerator / MaxInt64);End;  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. ---------------------------------------------------------------------- fiscraliger · 2024-04-08 17:09 Quote from: TRon on April 07, 2024, 05:49:00 pmMore 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. PRNG creates signed 64-bit values, i.e. any random value from -9223372036854775808 to 9223372036854775807. ---------------------------------------------------------------------- Thaddy · 2024-04-08 17:27 So that is a double value and not an extended value? Then it is easy. ---------------------------------------------------------------------- wp · 2024-04-08 17:28 Maybe a stupid question: Why do you need random extended values when your random number generator cannot provide 80 bits? Double would be sufficient. [EDIT] Thaddy was faster... ---------------------------------------------------------------------- Thaddy · 2024-04-08 17:30 Quote from: wp on April 08, 2024, 07:28:16 pmMaybe a stupid question: Why do you need random extended values when your random number generator cannot provide 80 bits? Double would be sufficient. [EDIT] Thaddy was faster... [edit] And I explained it in my first post. Now it is clear he simply wants a double. ---------------------------------------------------------------------- fiscraliger · 2024-04-08 17:35 Quote from: wp on April 08, 2024, 07:28:16 pmMaybe a stupid question: Why do you need random extended values when your random number generator cannot provide 80 bits? Double would be sufficient. [EDIT] Thaddy was faster... Quote from: Thaddy on April 08, 2024, 07:27:17 pmSo that is a double value and not an extended value? Then it is easy. I need Extended random value because other code accepts only input of type Extended. I do not want to pass Double as extended, because this produces problems in receiving code. Speaking in terms of precision, I need 18 guaranteed random digits after the decimal point. If 80 random bits are needed, I can provide that from PRNG I use, concatenating bits in record (I already tried that, knowing that Extended = record Mantissa:int64; Exponent:Word end;, but I failed with subsequent processing). ---------------------------------------------------------------------- Thaddy · 2024-04-08 18:08 The precision is charted in this table from the manual: https://www.freepascal.org/daily/doc/ref/refsu5.html#x27-310003.1.2 That means you indeed - given your 18 decimals - need a 80 bit resolution. (actually, in practice 79, otherwise you run into NaN in some cases) I suspected that from the beginning.... But how come you start off with 64 bit data? That makes no sense.... ---------------------------------------------------------------------- tetrastes · 2024-04-08 21:22 Quote from: Thaddy on April 08, 2024, 08:08:28 pmBut how come you start off with 64 bit data? That makes no sense.... Why not? Consider this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  EMaxInt64: extended = extended(9223372036854775808); var  i, j: int64;  e: extended; begin  j := 0;  repeat    i := 9223372036854775807 - j;    e := i / EMaxInt64;    writeln(e);    j := j+1;  until j = 10;end.  Output is (on systems supporting true 80-bit extended, of course):  9.99999999999999999892E-0001  9.99999999999999999783E-0001  9.99999999999999999675E-0001  9.99999999999999999566E-0001  9.99999999999999999458E-0001  9.99999999999999999349E-0001  9.99999999999999999241E-0001  9.99999999999999999133E-0001  9.99999999999999999024E-0001  9.99999999999999998916E-0001 ---------------------------------------------------------------------- tetrastes · 2024-04-08 22:28 Quote from: fiscraliger on April 08, 2024, 11:41:51 amI did this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MaxInt64 := Power(2, 63); function Random_extended:extended;Var Numerator:Int64;BeginNumerator := MyPRNG_SignedRandom64;Result := Numerator / MaxInt64;End;And what's wrong with that? I suppose that MaxInt64 is extended? Quote from: fiscraliger on April 08, 2024, 11:41:51 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Random_extended_positive_only:extended;Var Numerator:Int64;BeginNumerator := MyPRNG_SignedRandom64;Result := Abs(Numerator / MaxInt64);End;  As engineer I would suggest Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result := (Numerator / MaxInt64 + 1) * 0.5; ---------------------------------------------------------------------- fiscraliger · 2024-04-10 08:39 Quote from: tetrastes on April 09, 2024, 12:28:04 am Quote from: fiscraliger on April 08, 2024, 11:41:51 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Random_extended_positive_only:extended;Var Numerator:Int64;BeginNumerator := MyPRNG_SignedRandom64;Result := Abs(Numerator / MaxInt64);End;  As engineer I would suggest Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result := (Numerator / MaxInt64 + 1) * 0.5; Could you be so kind and explain this formula? ---------------------------------------------------------------------- tetrastes · 2024-04-10 10:10 The task is to convert numbers from -1...1 to 0..1 interval. (-1+1)*0.5=0, (1+1)*0.5=1, (0+1)*0.5=0.5, etc... PS. This method preserves distribution, but for random numbers it is probably not important, as the distribution for "true" random numbers is constant (horizontal line). ---------------------------------------------------------------------- Thaddy · 2024-04-10 10:16 Would that not mean you need an unsigned type for the devisor? ---------------------------------------------------------------------- tetrastes · 2024-04-10 10:36 Quote from: Thaddy on April 10, 2024, 12:16:09 pmWould that not mean you need an unsigned type for the devisor? It's no matter, how numbers in -1...1 interval were obtained. In this specicfic example in the OP's code the divisor is positive: extended MaxInt64 := Power(2, 63), but it also can be negative (-Power(2, 63)), for his (supposed to be "true") Quote from: fiscraliger on April 08, 2024, 07:09:13 pmPRNG creates signed 64-bit values, i.e. any random value from -9223372036854775808 to 9223372036854775807. the results will be the same (neglecting [-1..1) and (-1..1] difference). ---------------------------------------------------------------------- fiscraliger · 2024-04-10 14:10 @tetrastes - thank you for your help and explanations. Especially your Reply #21 was very informative.