Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Generate Random Point in a Circle

Posted on August 5, 2019July 26, 2020 by braindenny

Generate Random Point in a Circle



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #random

Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle.

Note:

  1. input and output values are in floating-point.
  2. radius and x-y position of the center of the circle is passed into the class constructor.
  3. a point on the circumference of the circle is considered to be in the circle.
  4. randPoint returns a size 2 array containing x-position and y-position of the random point, in that order.

Example 1:

Input: 
["Solution","randPoint","randPoint","randPoint"]
[[1,0,0],[],[],[]]
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

Example 2:

Input: 
["Solution","randPoint","randPoint","randPoint"]
[[10,5,-7.5],[],[],[]]
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution’s constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments. Arguments are always wrapped with a list, even if there aren’t any.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/generate-random-point-in-a-circle
// Basic Ideas:
//  Generate two rand value from [0, 2*r]. 
//  Denotes as (x, y)
//  If (x-r)*(x-r)+(y-r)*(y-r)>r*r, regenerate the pair
//
// Complexity: Time ?, Space O(1)
import "math/rand"
type Solution struct {
    radius float64
    x_center float64
    y_center float64
}

func Constructor(radius float64, x_center float64, y_center float64) Solution {
    return Solution{radius:radius, x_center:x_center, y_center:y_center}
}

func (this *Solution) RandPoint() []float64 {
    r := this.radius
    x, y := 2*r, 2*r
    for (x-r)*(x-r) + (y-r)*(y-r)>r*r {
        x, y = rand.Float64()*2*r, rand.Float64()*2*r
    }
    return []float64{x-r+this.x_center, y-r+this.y_center}
}

/**
 * Your Solution object will be instantiated and called as such:
 * obj := Constructor(radius, x_center, y_center);
 * param_1 := obj.RandPoint();
 */
linkedin
github
slack

Post Views: 0
Posted in MediumTagged random

Post navigation

Series: Geometry Problems & Follow-up
LeetCode: Wildcard Matching

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.