Forum > General

Random values of Extended type

<< < (2/6) > >>

fiscraliger:

--- Quote from: KodeZwerg 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.

--- End quote ---

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:

--- Quote from: fiscraliger on April 06, 2024, 11:54:05 pm ---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.

--- End quote ---
Can't you simply divide your prng by 1 shl 63 (unsigned) to calculate a -1..1 range ?

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:
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:

--- Quote from: Thaddy on April 07, 2024, 07:35:45 am ---Depending on application this may or may not be an issue.

--- End quote ---
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

--- End quote ---
...is bogus based on the first condition.

--- Quote from: jamie on April 07, 2024, 04:07:20 pm ---Ok, the "," through me off !

--- End quote ---
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.