943,910 Members | Top Members by Rank

Ad:
Nov 10th, 2007
0

help with random numbers in pascal

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pukepuke is offline Offline
3 posts
since Nov 2007
Nov 10th, 2007
0

Re: help with random numbers in pascal

If you don't want the same random numbers over and over again, at the very beginning of your program, call the randomize procedure.
Delphi Syntax (Toggle Plain Text)
  1. begin
  2. randomize; // init number generator from system clock
  3. ...
  4. 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.
Last edited by Duoas; Nov 10th, 2007 at 11:12 am.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 10th, 2007
0

Re: help with random numbers in pascal

I thought about this, just would like to know if there was a specific command for what I need. Thank you very much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pukepuke is offline Offline
3 posts
since Nov 2007
Nov 10th, 2007
0

Re: help with random numbers in pascal

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).
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 12th, 2007
0

Re: help with random numbers in pascal

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Artmann is offline Offline
19 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Newbie needs help... ;)
Next Thread in Pascal and Delphi Forum Timeline: Returning arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC