How to get random number in pascal?

I want to get a random number in pascal from a range. Basically something like this:

r = random(100,200); 

The code above will have a random number from 100 to 200.

Any ideas?

The built-in pascal function allows you to get a number from 0 to your range, while I need to specify a minimum number to return

+6
pascal
source share
5 answers

Just get a random number with the correct range (i.e. from 100 to 200 there will be a range of 100), then add the initial value to it

So: random(100) + 100 for your example

+9
source share

As already stated, you should use

 myrandomnumber := random(span) + basenumber; 

However, to get quality random numbers, you have to call

 randomize(); 

Once, when starting the application, initialize the random number generator.

+3
source share

Could you just declare the start variable and the end variable and pass random data? eg.

 var varMyRandomNumber, x, y := extended; begin x := 100; y := 200; varMyRandomNumber := random(x,y); ShowMessage(IntToStr(varMyRandomNumber)); end; 

?

Here is a good example of using a for loop to set the start and end values: http://www.freepascal.org/docs-html/rtl/system/random.html

+2
source share

First of all, I recommend using Randomize at the beginning of the program (it changes the number selection algorithm).

To get a random number between two numbers, you need the following:

 Result:=Min+random(10000)mod max + 1; 

I donโ€™t remember the maximum value for random, so you can change it (it does not change anything).

Using 'mod', you get the module from the Random and max sections. +1 is necessary because you will never get the number = max, only the number that = max-1, so you need to write +1.

Good luck

-one
source share

You can do it like Int: = Random (100); he gives 100 random numbers. then when you show it or use it just add 101 to this integer, so its between 100 and 200

-one
source share

All Articles