help with random numbers in pascal

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 3
Reputation: pukepuke is an unknown quantity at this point 
Solved Threads: 0
pukepuke pukepuke is offline Offline
Newbie Poster

help with random numbers in pascal

 
0
  #1
Nov 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: help with random numbers in pascal

 
0
  #2
Nov 10th, 2007
If you don't want the same random numbers over and over again, at the very beginning of your program, call the randomize procedure.
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: pukepuke is an unknown quantity at this point 
Solved Threads: 0
pukepuke pukepuke is offline Offline
Newbie Poster

Re: help with random numbers in pascal

 
0
  #3
Nov 10th, 2007
I thought about this, just would like to know if there was a specific command for what I need. Thank you very much.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: help with random numbers in pascal

 
0
  #4
Nov 10th, 2007
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).
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: Artmann is an unknown quantity at this point 
Solved Threads: 0
Artmann Artmann is offline Offline
Newbie Poster

Re: help with random numbers in pascal

 
0
  #5
Nov 12th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1331 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC