0

I need a program that generates a nxn matrix with random numbers, but they're not completely random. They must be delimited in -20,0 < x < 20,0, for example, but all i searched for the Random functions in pascal says that it produce numbers in a interval starting in zero...so, i would like to know how do i generate random numbers in a -n < x < n interval.

3
Contributors
4
Replies
8
Views
10 Years
Discussion Span
Last Post by Artmann
0

If you don't want the same random numbers over and over again, at the very beginning of your program, call the randomize procedure.

begin
  randomize;  // init number generator from system clock
  ...
end.

As for the range, the problem is purely mathematical. If you have a list of seven numbers (0, 1, 2, 3, 4, 5, 6), what do you have to do to it to make it (-3, -2, -1, 0, 1, 2, 3)?

Hope this helps.

0

I thought about this, just would like to know if there was a specific command for what I need. Thank you very much.

0

No, the only function is random. If you say x = random( 7 ); then x will be one of (0, 1, 2, 3, 4, 5, 6).

0

This program will generate a random number between -20 and 20.

program RandomX;

var x : Integer;

begin

       Randomize;
       x := (Random(40)-20);     // -20 <= x < 20
 
end.

Hoped this helped.

~ Artmann

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.