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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:44:14+00:00 2026-05-18T00:44:14+00:00

The range for x and y is from 0 to 99. I am currently

  • 0

The range for x and y is from 0 to 99.

I am currently doing it like this:

excludeFromTrainingSet = []
while len(excludeFromTrainingSet) < 4000:
    tempX = random.randint(0, 99)
    tempY = random.randint(0, 99)
    if [tempX, tempY] not in excludeFromTrainingSet:
        excludeFromTrainingSet.append([tempX, tempY])

But it takes ages and I really need to speed this up.

Any ideas?

  • 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-18T00:44:15+00:00Added an answer on May 18, 2026 at 12:44 am

    Vincent Savard has an answer that’s almost twice as fast as the first solution offered here.


    Here’s my take on it. It requires tuples instead of lists for hashability:

    def method2(size):
        ret = set()
        while len(ret) < size:
            ret.add((random.randint(0, 99), random.randint(0, 99)))
        return ret
    

    Just make sure that the limit is sane as other answerers have pointed out. For sane input, this is better algorithmically O(n) as opposed to O(n^2) because of the set instead of list. Also, python is much more efficient about loading locals than globals so always put this stuff in a function.

    EDIT: Actually, I’m not sure that they’re O(n) and O(n^2) respectively because of the probabilistic component but the estimations are correct if n is taken as the number of unique elements that they see. They’ll both be slower as they approach the total number of available spaces. If you want an amount of points which approaches the total number available, then you might be better off using:

    import random
    import itertools
    
    def method2(size, min_, max_):
        range_ = range(min_, max_)
        points = itertools.product(range_, range_)
        return random.sample(list(points), size)
    

    This will be a memory hog but is sure to be faster as the density of points increases because it avoids looking at the same point more than once. Another option worth profiling (probably better than last one) would be

    def method3(size, min_, max_):
        range_ = range(min_, max_)
        points = list(itertools.product(range_, range_))
    
        N = (max_ - min_)**2
        L =  N - size
        i = 1
        while i <= L:
            del points[random.randint(0, N - i)]
            i += 1
        return points
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently my jQuery slider prevents the user from progressing if the range is 0-0
I'm copying a range from the page and pasting it back elsewhere, but this
I want to block an entire ip range from my webserver but I'm not
I created a test application that generates 10k random numbers in a range from
Suppose I have range from E2 to E16. How do I read values from
I have range of numbers that range from 1 - 0.00000X . Most are
The documentation states: Precision can range from 1 to 38. Scale can range from
I've looked through many similar threads which seem to range from changing settings to
You are given 2^32-2 unique numbers that range from 1 to 2^32-1. It's impossible
In IPv6, the prefix length can range from 0-128. What is the significance of

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.