How make Random Numbers (from,to) lazarus / 61-Beginners ---------------------------------------------------------------------- coradi · 2018-06-29 11:11 Hallo, how can I make Random number from to ... ? I mean Random (from 5 to 20); SOLVED :-) With Randomrange in the math lib/unit https://www.freepascal.org/docs-html/rtl/math/randomrange.html ---------------------------------------------------------------------- wp · 2018-06-29 11:26 Although you have a solution already - I have the feeling that you did not understand the priciple. The function Random() returns a value between 0 and 1. If you multiply this value by 15 (= 20- 5) you have a random number between 0 and 15. And when you add 5 you have a random number between 5 and 20. Or more general (assuming a < b) 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 RandomBetween(a, b: Double): Double;begin  Result := a + Random * (b - a);end; ---------------------------------------------------------------------- coradi · 2018-06-29 11:26 But why ist a := Randomrange(1,2); always smaler than 2?!? And NEVER 2? ---------------------------------------------------------------------- wp · 2018-06-29 11:41 because Random is never 1 (0 <= Random() < 1; my formulation above "between 0 and 1" was not quite correct...) [EDIT] Ah, I see in the sources that RandomRange is for integer arguments and result. My function above is for floating point. When you call RandomRange(1, 2) where the range is 1 (=2-1) it seems that you want a floating point result. In this case, RandomRange is not good for you. ---------------------------------------------------------------------- VTwin · 2018-07-11 01:30 wp has given the answer. A small point in case you missed it, the function: Randomize; should be called once in a program to initialize the random number generator. The function x := Random; returns x = 0.0 to x = < 1.0 using a "Mersenne Twister" algorithm, see the docs. ---------------------------------------------------------------------- wp · 2018-07-11 07:13 Quote from: VTwin on July 11, 2018, 03:30:55 amA small point in case you missed it, the function: Randomize; should be called once in a program to initialize the random number generator. It depends on what you need the random numbers for. Just to populate an array of float with "any" numbers, in replacement of "real" numbers, Randomize is not required, it even makes debugging unnecessarily difficult because the values are different with every program run. In order to get reproducible random number, I also set the RandSeed to a fixed value sometimes. ---------------------------------------------------------------------- graemejc · 2018-07-14 14:24 function irand(low,high:integer):Integer; { Integer RANDom } { Pre: } { Post: returns an integer in the range low...high, HENCE irand(1,2) returns 1 or 2. } begin { function irand }         irand:=low+random(high-low+1); end {function irand } ; ---------------------------------------------------------------------- Thaddy · 2018-07-14 15:32 Algorithm is given low <= r < high simply - call randomrange(5,20) - call random and add that to the value. range will be a double <= low < high 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 Uses  math;var i:integer;  begin randomize; // unless statistics for i := 0 to 99 do   writeln(randomrange(5,20)+random :3:8);end. I will provide a patch to add randomrange for double to math. ---------------------------------------------------------------------- VTwin · 2018-07-14 15:59 Quote from: wp on July 11, 2018, 09:13:38 amIt depends on what you need the random numbers for. Just to populate an array of float with "any" numbers, in replacement of "real" numbers, Randomize is not required, it even makes debugging unnecessarily difficult because the values are different with every program run. In order to get reproducible random number, I also set the RandSeed to a fixed value sometimes. Good point, thanks. ---------------------------------------------------------------------- Thaddy · 2018-07-14 16:26 Quote from: VTwin on July 14, 2018, 05:59:13 pmGood point, thanks. You may be interested in my wiki entries about random. ---------------------------------------------------------------------- VTwin · 2018-07-14 21:50 Quote from: Thaddy on July 14, 2018, 06:26:51 pmQuote from: VTwin on July 14, 2018, 05:59:13 pmGood point, thanks. You may be interested in my wiki entries about random. Thanks, Thaddy. I have looked at your page before, it is a very nice reference, thanks for making it available. I have had need of random numbers from a von Mises distribution, and came across the following useful link: https://amrandom.soft112.com/ I expect you were aware of it, but others may find it useful. It is based on code from Alan R. Miller. I still refer to his 1981 book "Pascal Programs for Scientists and Engineers" on occasion. That was back in the days when Pascal was being promoted for scientific applications. Press et al., 1989, Numerical Recipes in Pascal, was the first, and last, edition in Pascal.  Jean Debord's very fine numerical library DMath (developed as an LGPL alternative to NR), contains some useful random number functions and several generators: https://sourceforge.net/projects/dmath/ Again, I'm sure you are aware of this, but others may find it a useful link. EDIT: The original NR Pascal "shareware" code can be found here: http://archives.math.utk.edu/software/msdos/numerical.analysis/nrpas13 / I used to use it a lot, but the license is a little strange. I've now removed it in favor of DMath. ---------------------------------------------------------------------- Phil · 2018-07-14 22:13 Quote from: VTwin on July 14, 2018, 11:50:25 pmAgain, I'm sure you are aware of this, but others may find it a useful link. CreateGUID is also a good way of creating a random sequence. Particularly if you need to create a random temporary filename. In a multiprocessing context, CreateGUID is the only safe way to do it (specifically, GetTempFileName is not safe when multiprocessing). Unfortunately there's still a bug in CreateGUID in stable FPC on macOS and Linux, although fixed in trunk; okay in Windows. I believe on all platforms it's using the OS facilities for random number generation. https://bugs.freepascal.org/view.php?id=31555 ---------------------------------------------------------------------- VTwin · 2018-07-14 22:35 @Thaddy I just noticed your use of NaN return values. I'd have to do some testing, but I seem to get inconsistent results across platforms. It seems code that I develop on a Mac can crash on Windows when NaNs are generated, Windows seems less robust. I've not thoroughly investigated, but noticed some odd behavior. Perhaps you, or someone else, has more information? EDIT: Normally I code on Mac, then when problems occur on Win go back and find whatever may be the problem, divide by zero or whatever, and correct the code for that. ---------------------------------------------------------------------- Thaddy · 2018-07-15 06:46 Quote from: VTwin on July 15, 2018, 12:35:47 am@Thaddy I just noticed your use of NaN return values. Can you give me an example that does that, I mean different results?: usually my code is tested on Linux (x86_64 and ARM), OSX and on Windows 64. It is supposed to give the same results on all platforms. So if it does not (given the same randseed) there is a bug and I need to fix it. ---------------------------------------------------------------------- Thaddy · 2018-07-15 12:55 @vtwin Yes I know most of them. I have a hard copy of numerical recipes (The Pascal one!!)  And a first edition of "TAOCP" which is rather a rare find. Note most of the PRNG's I published are invented beyond the '80's and - on a whole - with a better distribution than NR. (Test them with bigcrush! as per my wiki entry.) I can add some of the old school stuff, though: the newer ones are more like discoveries than science... Also note that HRNG's are much older. ---------------------------------------------------------------------- VTwin · 2018-07-15 13:03 Quote from: Thaddy on July 15, 2018, 08:46:02 amCan you give me an example that does that, I mean different results?: usually my code is tested on Linux (x86_64 and ARM), OSX and on Windows 64. It is supposed to give the same results on all platforms. So if it does not (given the same randseed) there is a bug and I need to fix it. It was a while back, but it seemed to me that NaN values were treated differently on different platforms. I develop on Mac, and then would run into problems on Win. I was trying to use NaN as a test value, and got inconsistent results. A user needed to enter explicit missing values as 'NaN' (or old school punch card '999'). For my own purposes I defined: 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    NAIOFloat : double  = -1.7e+308;   // explicit missing value, i/o = 'NaN'  NAFloat   : double  = -1.6e+308;   // missing value, i/o = '' { IsNA  'Is Not Available', returns true if number is not valid. }function  IsNA(r: double): boolean;begin  result := (r = NAFloat) or (r = NAIOFloat);end; { IsNAIO  'Is Not Available', returns true if number is explicitly not valid. Normally  user entered NaN or 999. }function  IsNAIO(r: double): boolean;begin  result := (r = NAIOFloat);end; { IsNum  Uses NAFloat, a very large magnitude negative double whose magnitude  is bigger than mMaxFloat. Use NAFloat to initialize doubles, and IsFloat  to test for a real value. Use -mMaxFloat for smallest double. }function IsNum(r: double): boolean;begin  result := (r > NAFloat);end; etc.  as work arounds. It seemed odd to me, as this was using a virtual machine, so the processor was identical. I will look into it further and try to give you a clear example. ---------------------------------------------------------------------- Thaddy · 2018-07-15 13:34 Well, the pseudo randoms I described are all based on bit-patterns (and Unsigned). Avoiding NaN (or Infinity) is therefor not represented in any Pascal (or C, C++)  code for those PRNG's I have seen. If it is important to you to avoid both NaN and Infinity, you could mask out the possibility by "and $EF. That WILL reduce your randomness to sqr("size of" -1). E.g. given 16 bit integer randomness is 15 bit. You will loose entropy. As long as you realize that, it should not be a practical problem. The HWRNG's can also generate NaN or Infinity for the same reason. So basically you can avoid both by a mask of the size of a native unsigned type and loose 1 bit precision. I do not know of any - guaranteed - other way, but this algorithm is cross-platform. ---------------------------------------------------------------------- VTwin · 2018-07-15 14:54 Quote from: Thaddy on July 15, 2018, 02:55:40 pm@vtwin Yes I know most of them. I have a hard copy of numerical recipes (The Pascal one!!)  And a first edition of "TAOCP" which is rather a rare find. Note most of the PRNG's I published are invented beyond the '80's and - on a whole - with a better distribution than NR. (Test them with bigcrush! as per my wiki entry.) I can add some of the old school stuff, though: the newer ones are more like discoveries than science... Also note that HRNG's are much older. I seem to have acquired four hard copies of NR, in C, Pascal and Fortran over the years, as well as several more recent electronic copies. It is too bad the licensing is not more flexible, I recently purged NR from my code in preparation to open source.  A first edition of TAOCP is a nice find, I'd pick that up if I found a copy. It is excellent to have more up to date algorithms available. I appreciate you pointing that out, I guess I should check to see if I am using older versions. I don't have a good understanding of RNGs, or follow the literature, so depend on experts such as yourself. I just need one sometimes, so like to have good code available. If you decide to add a few old school ones, that could be a nice addition. Also perhaps note which ones are modern improvements, so people like me don't continue to use dusty old code from the 80s. And, no, I could not have told you that HRNGs are much older, interesting though. ---------------------------------------------------------------------- VTwin · 2018-07-15 15:39 Quote from: Thaddy on July 15, 2018, 03:34:30 pmWell, the pseudo randoms I described are all based on bit-patterns (and Unsigned). Avoiding NaN (or Infinity) is therefor not represented in any Pascal (or C, C++)  code for those PRNG's I have seen. If it is important to you to avoid both NaN and Infinity, you could mask out the possibility by "and $EF. That WILL reduce your randomness to sqr("size of" -1). E.g. given 16 bit integer randomness is 15 bit. You will loose entropy. As long as you realize that, it should not be a practical problem. The HWRNG's can also generate NaN or Infinity for the same reason. So basically you can avoid both by a mask of the size of a native unsigned type and loose 1 bit precision. I do not know of any - guaranteed - other way, but this algorithm is cross-platform. Ok, thanks, I did not know that NaN and Infinity could be generated. So, for example, if I am generating random numbers from a Gaussian distribution, I should be checking? I assume the Math functions IsInfinite and IsNan should work to detect them instead of masking? I do not specifically remember a problem with RNGs and NaNs. Maybe I was doing it incorrectly, but I seemed to have encountered cross- platform differences in assigning and/or detecting NaNs. I'd have to try to recreate the issue. EDIT: Thanks, very interesting. It does make sense if it is based on bit patterns, and that masking will be quicker. ---------------------------------------------------------------------- Thaddy · 2018-07-15 16:05 Quote from: VTwin on July 15, 2018, 05:39:58 pmEDIT: Thanks, very interesting. It does make sense if it is based on bit patterns, and that masking will be quicker. Note the values itself are always valid. Performing calculations with them can give problems. ---------------------------------------------------------------------- VTwin · 2018-07-15 22:27 Quote from: Thaddy on July 15, 2018, 06:05:30 pmQuote from: VTwin on July 15, 2018, 05:39:58 pmEDIT: Thanks, very interesting. It does make sense if it is based on bit patterns, and that masking will be quicker. Note the values itself are always valid. Performing calculations with them can give problems. Makes sense! For anyone who is curious, I found an interesting article discussing IEEE NaN and Infinity: http://www.efg2.com/Lab/Mathematics/NaN.htm