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 8477381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:27:08+00:00 2026-06-10T18:27:08+00:00

I am trying to do following: Create 2 unique numbers that are both within

  • 0

I am trying to do following:

Create 2 unique numbers that are both within a certain range and they are at least n bigger/smaller.

In example:

Range is 0-600
Minimum “difference” is 150

So the generated numbers could be: [2,400],[120,310],[82,530]

But Not [900,400] or [200,220].

Thats what I have so far:

var posYArray   = [];

for(i=0; i < 2; i++){ 

    var posY    = (Math.random() * 200).toFixed();

    if(i < 1){
        posYArray.push(posY);
    }else{

        for(i=0; i < posYArray.length; i++){ 
            if(posY < posYArray[i]+100){
                posYArray.push(posY);
            }else{
                //Restart loop??
            }
        }

    }

}

But this randomly crashes the browser and also I didnt know a good way to restart the loop when the numbers are too close…

  • 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-06-10T18:27:10+00:00Added an answer on June 10, 2026 at 6:27 pm

    You can shift the gap in which not to choose a number, i.e. pick a random number x between 0 and range – gap, than pick first one between 0 and x and the second between x + gap and range. That would be somewhat more efficient.

    var range = 600, gap = 150;
    var x = Math.floor(Math.random() * (range - gap));
    var posX = (Math.random() * (x)).toFixed();
    var posY = (Math.random() * (range - x - gap) + x + gap).toFixed();
    

    Works in O(1).

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

Sidebar

Related Questions

i am trying to create an .htaccess file that will achieve the following create
I am trying to create an android app that has the following: Theme set
I'm trying to create a simple web page that resembles the following: I've got
I'm trying to optimize a procedure that has code like the following: CREATE TABLE
I'm trying the following: CREATE TABLE Table1 ( RecordNo autonumber, --error here! PersonId varchar(50),
i'm trying to create following layout: http://jsfiddle.net/BTuMH/1/ but without setting the width, like: http://jsfiddle.net/yuGyg/
I'm trying to create a new function in SQL with the following code: CREATE
I am trying to figure out how to accomplish the following task: Create a
I'm trying to duplicate a table using phpmyadmin. It generates the following query: CREATE
New with Android. Sorry! I'm trying to create the following... Wait for Timer to

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.