11

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

r = random(100,200);

The above code would then have a random number between 100 and 200.

Any ideas?

The built in pascal function only lets you get a number from between 0-your range, while i need to specify the minimum number to return

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 5
    Wow! Someone still uses Pascal?! I haven't used that since the early 1980s. – wallyk Feb 11 '11 at 05:35
  • @wallyk pls see if you could answer my other question :). http://stackoverflow.com/questions/4965977/pascal-syntax-error – Ali Feb 11 '11 at 05:40

6 Answers6

13

Just get a random number with the correct range (ie 100 to 200 would be range 100) then add the starting value to it

So: random(100) + 100 for your example

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Ralph
  • 438
  • 6
  • 17
5

As already pointed out, you should use

myrandomnumber := random(span) + basenumber;

However, to get better quality random numbers, you should call

randomize();

once, on start of your application, to initialize the random number generator.

Tuncay Göncüoğlu
  • 1,699
  • 17
  • 21
2

Couldn't you just declare a starting variable and an end variable and pass random those? e.g.

var
varMyRandomNumber, x, y := extended;

begin

x := 100;
y := 200;

varMyRandomNumber := random(x,y);
ShowMessage(IntToStr(varMyRandomNumber));
end;

?

There's a good example here of using a for loop to set starting and end values : http://www.freepascal.org/docs-html/rtl/system/random.html

Gizmo_the_Great
  • 979
  • 13
  • 28
0

Use RandomRange or RandomFrom:

function RandomRange(const aFrom: Integer; const aTo: Integer): Integer;

RandomFrom returns a random element from the array AValues. The return value has the same type as the type of the array elements.

Robert
  • 1,357
  • 15
  • 26
-1

first of all, i recommend you to use Randomize at the beginning of the program (it changes the algorithm of selecting the number).

To get a random number between some two numbers you need this:

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

I don't remember the maximum value for random, so you can change it (it don't changes anything).

By using 'mod' you get module from division Random and max. +1 is needed, because you never get the number that = max, only the number that =max-1, so you need to write +1.

Good luck!

proggamer12
  • 192
  • 10
  • In that case you also need `min - 1` else you'll never get `min`. But in general in programming when we say "between X and Y" we mean "between X inclusive and Y exclusive" - this simplifies a bunch of stuff so the formula is just min + random(span), no need for a +1. Also your bound of 10000 is arbitrary and won't work for all values of `max`, you should do `random(max - min)`. For a general purpose solution you also want to check that `min <= max` else things will break. – Thomas Jul 14 '13 at 09:59
-2

You can make it like Int:=Random(100); it give's 100 random numbers. then when you display it or use it just add 101 to that integer so its between 100 and 200