Recent

Author Topic: Random Number  (Read 2523 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Random Number
« on: December 16, 2019, 09:35:48 am »
I need a random Integer < 7500 and > 2800. This is the way I doing it now but was wondering if there isn't a better way?

Code: Pascal  [Select][+][-]
  1. Repeat
  2.        X0 := Random(7500);
  3.        if X0 > 2800 then begin Done := True; end;
  4.        Until Done;                              
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

vangli

  • New Member
  • *
  • Posts: 44
Re: Random Number
« Reply #1 on: December 16, 2019, 09:45:09 am »
Regards Bent

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Random Number
« Reply #2 on: December 16, 2019, 09:59:09 am »
Thanks I'll test it out.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Random Number
« Reply #3 on: December 16, 2019, 10:39:58 am »
I need a random Integer < 7500 and > 2800. This is the way I doing it now but was wondering if there isn't a better way?
Simplify the problem: Suppose, you call Random(4). This will return the numbers 0, 1, 2 or 3. Now add 10, i.e. Random(4)+10 - you will get numbers 10, 11, 12, or 13. In general, when you need random integers from the interval [n1..n2] you put the difference of the range limits plus 1 into the argument of Random (--> Random(n2-n1+1)), and you must add the lower limit of the interval (--> n1 + Random(n2-n1+1)). The "plus 1" is required because Random(n) returns values from 0 to n-1.

So, which number must be the argument of the Random() function, and which number must be added so that the overall output is a value from the interval [2801...7499]? (I hope you really mean <7500, and not <=7500; the same with 2800).
« Last Edit: December 16, 2019, 01:42:11 pm by wp »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Random Number
« Reply #4 on: December 16, 2019, 10:52:49 am »
https://www.freepascal.org/docs-html/rtl/math/randomrange.html

As usual it is not a crime to read the official manuals....

It bothers me a bit that even distinguished forum members do not do so.
« Last Edit: December 16, 2019, 10:55:50 am by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Random Number
« Reply #5 on: December 16, 2019, 11:32:02 am »
Thaddy, I think that it is more instructive to guide the user to be able to create a solution based on his knowledge, than telling him to use a black-box procedure. In particular here where the "own" solution is so easy. RandomRange, however, cannot be understood without looking at the source code or reading the manual (and hoping that it is correct). The problem is: how is the second parameter handled? Is it excluded from the result set (as in Random(n)), or is it included as in Math.InRange(n1, n2) or Math.EnsureRange(n, n1, n2)?
« Last Edit: December 16, 2019, 01:43:46 pm by wp »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Random Number
« Reply #6 on: December 16, 2019, 06:32:35 pm »
@WP
So if I'm reading you right it would be Random(7500 - (2800 + 1)). I'll give it a try.
Random(n2-n1+1))

Seems I read somewhere you have to call x := Random() to initialize the generator.


@Thaddy

I don't think your comment about distinguished members not reading the manual applies to me. I'm not a distinguished member of the board.

But I did some reading about the Random generators. There is a whole bunch of them. And I saw where you wrote a Random generator package. But I never saw where it was explained like Random(n2-n1+1)).

Reading all that stuff was like trying to drink from a fire hose.

Thanks

   
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Random Number
« Reply #7 on: December 16, 2019, 06:44:17 pm »
Seems I read somewhere you have to call x := Random() to initialize the generator.
No, a single call to Randomize initialises the generator (perhaps in your main form's OnCreate handler).

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Random Number
« Reply #8 on: December 16, 2019, 06:54:21 pm »
Never forget the pernicious https://xkcd.com/221/ :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Random Number
« Reply #9 on: December 16, 2019, 07:20:07 pm »
So if I'm reading you right it would be Random(7500 - (2800 + 1)). I'll give it a try.
Random(n2-n1+1))

Not exactly. You said that you "need a random Integer < 7500 and > 2800". This means that the smallest number accepted will be 2801 -- this is n1. Similarly, the largest number accepted will be 7499 -- this is n2. Putting this into the formula above you get n2 - n1 + 1 = 7499 - 2801 + 1 = 4699 for the argument of the Random() function -- this means that Random(4699) will create numbers starting at 0 and ending with 4698 (1 less than the argument!). To get the range correct, you must add the 2801. Now the smallest number is 0 + 2801 = 2801, and the largest number is 4698 + 2801 = 7499. In total you must call
Code: Pascal  [Select][+][-]
  1.  X0 := 2801 + Random(4699);

Or, using the RandomRange mentioned by Thaddy you call
Code: Pascal  [Select][+][-]
  1. uses math;
  2. ...
  3.   X0 := RandomRange(2801, 7500);
Note here that the first argument is incremented because the value is a possible result of the function; if you would not do this your condition that the random integer must be > 2800 (not >= !!!) would be violated. And for the second argument you must know that it is never returned by the function -- the largest returned value is smaller by 1. Therefore, using the value 7500 is ok to fulfill the requirement < 7500.

A bit tricky either way...

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Random Number
« Reply #10 on: December 16, 2019, 07:28:55 pm »
Hi !

If you like a special row of random numbers you can set
Code: Pascal  [Select][+][-]
  1. randseed := 28101854;

randseed is a cardinal

And the simple solution of JLWests question is

Code: Pascal  [Select][+][-]
  1. x0 := random (7500 - 2800 -1) + 2800;

Winni



wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Random Number
« Reply #11 on: December 16, 2019, 07:51:37 pm »
And the simple solution of JLWests question is

Code: Pascal  [Select][+][-]
  1. x0 := random (7500 - 2800 -1) + 2800;
Again, not exactly. He is requesting >2800, but your smallest random number would be equal to 2800. And the upper limit is requested as "< 7500", i.e. the largest number should be equal 7499 - your largest number, however, is 7498 because the argument of the random function is never returned as a result.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Random Number
« Reply #12 on: December 16, 2019, 10:04:03 pm »
Thanks - Got it
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Random Number
« Reply #13 on: December 17, 2019, 12:15:06 am »
Okay. Here it is:

+1


But I am not the only one with this mistake. For a long time the behaviour of the Boeing 737 was at start and touchdown defined  for < 60 knots and > 60 knots. But not for = 60 knots. The error had not great effect because they were always only half a second at this speed ...

Winni

« Last Edit: December 17, 2019, 12:18:26 am by winni »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Random Number
« Reply #14 on: December 17, 2019, 04:58:20 am »
The Boeing 737 even with all it troubles hauls more passenger per day that all other commercial aircraft combined.

It just difficult to beat it's Seat @ mile cost and maintenance @ Hour cost.

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018