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

  • SEARCH
  • Home
  • 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 7975403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:37:46+00:00 2026-06-04T08:37:46+00:00

I want to generate 2n-1 random integers in the range [1,n] with each element

  • 0

I want to generate 2n-1 random integers in the range [1,n] with each element appearing twice except for random value, which only appears once.
for example:

n = 3  
seq = [1, 2, 3, 1, 3]  

in this example 2 appears only once.

My algorithm is to use dictionary, like this:

-------------
| num |times|
|  1  |  2  |
|  2  |  1  |
|  3  |  2  |

where the keys are from 1 to n and the value represents the number of occurrences of the key. I fill the dictionary with values of two and reduce the value to 1 for one random key.

  1. Is there any other algorithm?
  2. how to do it if n is very large lead to can not be stored in memory?
  • 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-04T08:37:47+00:00Added an answer on June 4, 2026 at 8:37 am

    I am not sure that I am 100 sure what you are after, but here is a try:

    import random as rn
    
    x = range(3)*2 #generate a list where each number appears twice
    
    rn.shuffle(x) #shuffle it
    x.pop()       #remove one number
    

    Result:

    >>> x
    [2, 0, 2, 1, 0] #the result is a list where every number appears twice, except for
                    #one number which was removed at random, also the numbers are 
                    #randomly arranged
    

    EDIT:

    Here is a try to do this for very large n (an n that a list of that size cannot be stored in your ram). I cannot see how to shuffle the integers. However, I can remove one at random. Let’s say you want to write the list to txt file.

    drop = rn.range(0,n) #choose a random integer to drop
    
    with open('my_file.txt','w') as f:
        for ind,ele in enumerate(xrange(n)):  
            if ind == drop: #do not write the element to txt file
                pass
            else:
                f.write(str(ele) + '\n') #write every except for one element to txt file
    
    with open('my_file.txt','a') as f:
        for ele in xrange(n):
            f.write(str(ele) + '\n') # write every element to txt file
    

    In the end we were wrote n-1 element to txt file two times, and 1 element once, that element was chosen at random.

    For n = 5 the txt file looks like this:

    0
    2
    3
    4
    0
    1
    2
    3
    4
    

    In the above case 1 is only showing up once, every other number is showing up twice.

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

Sidebar

Related Questions

Simple enough question: I'm using python random module to generate random integers. I want
I want to generate random colours which can be attractive in pie charts.I have
I want to generate random numbers with a range (n to m, eg 100
I want to generate random number, which is 9 digits including leading zero if
I want to generate random number in a specific range. (Ex. Range Between 65
Given an array of size n I want to generate random probabilities for each
I want to generate a random number of 14 positive digits only and I
I want to generate a random Integer that is: Inside the range [Min, Max]
I want to generate random numbers within a range and the generated numbers should
I want to generate a random number with in two range, and i want

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.