Forum > General

Random values of Extended type

(1/6) > >>

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

wp:

--- Quote from: KodeZwerg on April 06, 2024, 08:59:41 pm ---Tested with input from -1 up to 1 ...

--- End quote ---
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:
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:
Words... For me, "input" was the random number which was transformed, for you it is AMin and AMax. Alright.