English 中文(简体)
如何在条件条件下随机生成 num
原标题:how to generate random num with conditions

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]  

在本例2中,仅出现一次。

我的算法是使用字典,像这样:

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

键在 1 到 n 之间, 值代表键的发生次数。 我填入字典时输入了两个值, 并将一个随机键的值减为 1 。

  1. Is there any other algorithm?
  2. how to do it if n is very large lead to can not be stored in memory?
最佳回答

我不确定我百分百确定你在找什么 但这里要尝试一下:

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

结果:

>>> 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

编辑:

在此尝试对非常大的 n ( 无法将如此大小的列表存储在您的 RAME 中) 进行此操作 。 我无法看到如何冲洗整数 。 然而, 我可以随机删除一个 。 换句话说您想要将列表写入 txt 文件 。

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) +  
 ) #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) +  
 ) # write every element to txt file

最后,我们被写成 n-1 元素两次到 txt 文件, 1 元素一次, 该元素是随机选择的 。

对于 n= 5, txt 文件看起来像这个 :

0
2
3
4
0
1
2
3
4

在上述情况中,1只出现一次,其他数字出现两次。

问题回答

@Akavall 和 @Akavall 一样, 不知道我是否正确理解你。 您想要生成 2n-1 个数字, 范围在 1 到 n ( 包括 n I 假设) 之间 。 数字并不是 随机的, 只有 1 个数字 。

import random

n=3

# Generate n numbers
numbers = [i for i in range(1,n+1)]
# Concatenate list to itself (now have 2n numbers)
numbers *= 2
# Remove a random element in the list (now have 2n-1 numbers)
numbers.pop( random.randint(0, len(numbers)-1) )

# Print results
from collections import Counter
print( Counter(numbers) )

产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出

Counter({1: 2, 3: 2, 2: 1})

1) 我建议使用随机数字生成器来选择您的“ 单数” 编号, 然后我将使用一个包含您所有数字的集合, 然后用打乱的方法使用构建的打乱方法。 我建议使用打乱方法来构建, 因为以方法构建的方法通常会高度优化 。

(2) 如果 n 很大, 那么您可能想要将数字的块块写入文件, 并在任何特定时间只对部分进行打乱。 类似这一点可以同时打乱5个牌牌牌牌。 顶多是非常困难的, 但是您可以把大收藏的一部分拿起来, 把这些部分一起打乱, 将部分归还大收藏, 并选择另外两个部分来打乱, 重复, 直到您满足您想要的打乱要求 。

2. 如果 n 是无法存储在记忆中的大铅,如何做到这一点?

取决于您想要用这些数字做什么, 以及顺序是否重要。 从您展示表格I 的方式来判断, Id 表示您不在乎顺序, 所以即使有大 < code>n , 整个表格编码所需的实际信息数量非常小 : < code>n 本身和只有一个条目的索引。

最好彻底改变你的方法 如果你认为记忆会成为一个问题 但如果没有更多的信息 很难说

生成频率表生成器的频率表生成器,这将有助于处理内存问题

from random import randint

def generate_counts(n):
    remove_index = randint(1,n+1)
    return ((i+1,2-(remove_index==i)) for i in range(n))

产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出产出

for number, frequency in generate_counts(10):
    print "%i: %i"%(number,frequency)

1: 2
2: 2
3: 2
4: 1
5: 2
6: 2
7: 2
8: 2
9: 2
10: 2




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签