Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6195915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:33:13+00:00 2026-05-24T03:33:13+00:00

Generate a random point within a rectangle (uniformly) This suppose to be a simple

  • 0

Generate a random point within a rectangle (uniformly)

This suppose to be a simple problem.

However, in RANDOM_DATA homepage I found the following note:

However, we will not achieve uniform distribution in the simple case
of a rectangle of nonequal sides [0,A] x [0,B], if we naively scale
the random values (u1,u2) to (A*u1,B*u2). In that case, the expected
point density of a wide, short region will differ from that of a
narrow tall region. The absence of uniformity is most obvious if the
points are plotted.

I found it quite of strange… I can’t figure out why such scaling will affect the uniformity.

What am I missing?

Edit:

Thank you Patrick87 and missingno. I was searching for a theoretical reason for the statement. I now understand that the reason is not theoretical, but practical – the granularity of floating-point values.

If I’ll generate two uniform floating-points between 0 and 1 (which is an issue by itself due to the nature of floating-point value representation. Look here for an algorithm) – the granularity will be limited.

Suppose that there are X different values between 0 and 1. By scaling (u1,u2) to (u1,2*u2) we’ll have X different values in the range [0,u1] and X different values in the range [0,2*u2]. For area uniformity we should have twice as many different values in [0,2*u2] than in [0,u1].

Given that, Allow me to change my question:

How should I generate a random point within a rectangle (with uniform distribution by area)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:33:14+00:00Added an answer on May 24, 2026 at 3:33 am

    How should I generate a random point within a rectangle (with uniform distribution by area)?

    This should work:

      // given A, B, dimensions of rectangle
      // given g, granularity of shorter side
    
      if A > B then
         bm := g
         am := floor(g * A / B)
      else then
         am := g
         bm := floor(g * B / A)
    
      for i := 1 to n do
         av := A * RandomInt(0..am) / am
         bv := B * RandomInt(0..bm) / bm
         print (av, bb)
    

    EDIT: A simpler alternative would be to simply scale random floating point values by the same factor, choose points at random, and throw away points that fall outside your rectangle. However, you don’t know how many trials you’d need before you got N points in the rectangle…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to generate a random floating point number between 2 values. What
Could someone please point me toward a cleaner method to generate a random enum
I need to generate multiple random values under SQL Server 2005 and somehow this
I need to generate random tokens so that when I see them later I
I need to generate random text strings of a particular format. Would like some
I'm trying to generate random permutations of an 80-character fixed string in C. Much
I want to generate random numbers manually. I know that every language have the
Is there a way to generate random number on Windows by reading from a
What is the best way to generate random numbers using Objective-C on iOS? If
I need to generate a random integer between 1 and n (where n is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.