954,228 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with random numbers in pascal

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.

pukepuke
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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

pukepuke
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

No, the only function is random. If you say
x = random( 7 );

thenx will be one of (0, 1, 2, 3, 4, 5, 6).

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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

Artmann
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You