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.

Recommended Answers

All 4 Replies

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.

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

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).

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.