What is some good TI-BASIC code for randomly generating a point on the boundary of a square? Assume the square's vertices are at (0,0), (0,1), (1,0), and (1,1) (I'll try and generalize myself), and the point to be generated is stored on (A,B).

I assume I'd have to use rand and some conditional, but I can't begin to guess a nice compact way to do it.

To be clear, I don't mean inside. I mean exactly on the boundary. To avoid confusion.
Found a version.

EDIT:
never mind it's stupid
If the output being in a list variable is okay/desired, I've got this:

Code:
rand(2→L₁
int(2rand→L₁(randInt(1,2
Thanks Runer! I'm having trouble seeing how to generalize it, though. I have a formula for where (A,B) and (C,D) are opposite points on a rectangle, generating a random ordered pair on the rectangle:


Code:
int(2rand
{A+(C-A)(randAns+not(Ans)int(2rand)),B+(D-B)(randnot(Ans)+Ansint(2rand


Any way to improve this? I realize for my special case of a square, it can be simplified a bit.
Assuming a coordinate system (rectangle) from A to X and from B to Y, this code picks a random point on it:

Code:
(X-A)rand+A->PointX:(Y-B)rand+B-Y->PointY
Nik wrote:
Assuming a coordinate system (rectangle) from A to X and from B to Y, this code picks a random point on it:

Code:
(X-A)rand+A->PointX:(Y-B)rand+B-Y->PointY

I meant, on the boundary. A point inside a boundary is easy Razz
Generate a point inside the square and scale it so it lies on the boundary:


Code:
.5-rand(2
.5+.5Ans/max(abs(Ans


I don't know how to elegantly generate points on the rectangle given by (A,B) and (C,D).
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement