Random numbers in the range

This is in Delphi (more precisely, 7). How can I generate random numbers in a specific range? Similar to random.randint(1,6) in Python. I am trying to simulate rolling bones. Another option is to somehow exclude 0.

I currently have:

 Randomize; Roll := Random(7); Label3.Caption := IntToStr(Roll); 
+8
random numbers delphi range
source share
3 answers

you can use

 RandomRange(1, 7) 

which will return a random integer from the set {1, 2, 3, 4, 5, 6}.

( uses Math )

[By the way, it is trivial to "somehow exclude" zero. Just do Random(6) + 1 ]

+22
source share

In addition, you can create a value from the range float [a, b), with the exception of b:

 r := random; x := (ba)*r + a; 

The first line generates a value from [0; 1) interval; the second gives the value from [a, b).

If you want to get N random values ​​in the interval [a; b] (for example, 5 random values ​​from the interval [1; 2]: {1, 1.25, 1.5, 1.75, 2}) use the following:

 r := RandomRange(0, N-1); x := a + r*(ba)/(N-1); 
+2
source share

to a specific value that you can use

 randomize; ran:=random(6)+1; 

this value will be randomized from 1 to 6

+1
source share

All Articles