* * *

Author Topic: How make Random Numbers (from,to)  (Read 1873 times)

coradi

  • New member
  • *
  • Posts: 43
How make Random Numbers (from,to)
« on: June 29, 2018, 01:11:38 pm »
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
« Last Edit: June 29, 2018, 01:16:04 pm by coradi »

wp

  • Hero Member
  • *****
  • Posts: 4904
Re: How make Random Numbers (from,to)
« Reply #1 on: June 29, 2018, 01:26:30 pm »
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]
  1. function RandomBetween(a, b: Double): Double;
  2. begin
  3.   Result := a + Random * (b - a);
  4. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

coradi

  • New member
  • *
  • Posts: 43
Re: How make Random Numbers (from,to)
« Reply #2 on: June 29, 2018, 01:26:54 pm »
But why ist
a := Randomrange(1,2);

always smaler than 2?!?
And NEVER 2?

wp

  • Hero Member
  • *****
  • Posts: 4904
Re: How make Random Numbers (from,to)
« Reply #3 on: June 29, 2018, 01:41:32 pm »
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.
« Last Edit: June 29, 2018, 02:00:10 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

VTwin

  • Sr. Member
  • ****
  • Posts: 448
  • Former Turbo Pascal 3 user
Re: How make Random Numbers (from,to)
« Reply #4 on: July 11, 2018, 03:30:55 am »
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.
« Last Edit: July 11, 2018, 03:51:02 am by VTwin »
“Talk is cheap. Show me the code.” Linus Torvalds

Lazarus 1.8.4, FPC 3.0.4
macOS 10.11.5 (32 bit Carbon, working on Cocoa) 
Windows 7 (64 bit)
Ubuntu 16.04.3 (64 bit)

wp

  • Hero Member
  • *****
  • Posts: 4904
Re: How make Random Numbers (from,to)
« Reply #5 on: July 11, 2018, 09:13:38 am »
A 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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

graemejc

  • New member
  • *
  • Posts: 17
Re: How make Random Numbers (from,to)
« Reply #6 on: July 14, 2018, 04:24:52 pm »
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

  • Hero Member
  • *****
  • Posts: 6731
Re: How make Random Numbers (from,to)
« Reply #7 on: July 14, 2018, 05:32:33 pm »
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]
  1. Uses
  2.   math;
  3. var i:integer;  
  4. begin
  5.  randomize; // unless statistics
  6.  for i := 0 to 99 do
  7.    writeln(randomrange(5,20)+random :3:8);
  8. end.

I will provide a patch to add randomrange for double to math.
« Last Edit: July 14, 2018, 05:34:39 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

VTwin

  • Sr. Member
  • ****
  • Posts: 448
  • Former Turbo Pascal 3 user
Re: How make Random Numbers (from,to)
« Reply #8 on: July 14, 2018, 05:59:13 pm »
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.

Good point, thanks.
“Talk is cheap. Show me the code.” Linus Torvalds

Lazarus 1.8.4, FPC 3.0.4
macOS 10.11.5 (32 bit Carbon, working on Cocoa) 
Windows 7 (64 bit)
Ubuntu 16.04.3 (64 bit)

Thaddy

  • Hero Member
  • *****
  • Posts: 6731
Re: How make Random Numbers (from,to)
« Reply #9 on: July 14, 2018, 06:26:51 pm »
Good point, thanks.
You may be interested in my wiki entries about random.
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

VTwin

  • Sr. Member
  • ****
  • Posts: 448
  • Former Turbo Pascal 3 user
Re: How make Random Numbers (from,to)
« Reply #10 on: July 14, 2018, 11:50:25 pm »
Good 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.

« Last Edit: July 15, 2018, 12:15:37 am by VTwin »
“Talk is cheap. Show me the code.” Linus Torvalds

Lazarus 1.8.4, FPC 3.0.4
macOS 10.11.5 (32 bit Carbon, working on Cocoa) 
Windows 7 (64 bit)
Ubuntu 16.04.3 (64 bit)

Phil

  • Hero Member
  • *****
  • Posts: 2749
Re: How make Random Numbers (from,to)
« Reply #11 on: July 15, 2018, 12:13:31 am »
Again, 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

  • Sr. Member
  • ****
  • Posts: 448
  • Former Turbo Pascal 3 user
Re: How make Random Numbers (from,to)
« Reply #12 on: July 15, 2018, 12:35:47 am »
@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.
« Last Edit: July 15, 2018, 01:44:34 am by VTwin »
“Talk is cheap. Show me the code.” Linus Torvalds

Lazarus 1.8.4, FPC 3.0.4
macOS 10.11.5 (32 bit Carbon, working on Cocoa) 
Windows 7 (64 bit)
Ubuntu 16.04.3 (64 bit)

Thaddy

  • Hero Member
  • *****
  • Posts: 6731
Re: How make Random Numbers (from,to)
« Reply #13 on: July 15, 2018, 08:46:02 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.
« Last Edit: July 15, 2018, 09:01:13 am by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

Thaddy

  • Hero Member
  • *****
  • Posts: 6731
Re: How make Random Numbers (from,to)
« Reply #14 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!!)  8-) 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... 8-)
Also note that HRNG's are much older. <but you probably knew that  ::) >
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus