如何在今天的基础上生成不重复的随机数';java中的日期?

如何在今天的基础上生成不重复的随机数';java中的日期?,java,Java,我想每天生成一个唯一的随机索引来显示列表中N个单词的“每日单词”。 在每个单词都从列表中索引之前,我不希望重复相同的索引。例如,我在一个列表中有N个单词;每天的索引在N天内应该是不同的。以下是一些开始的步骤: 1) 生成一个随机数 2) 对照数组/哈希映射/列表/任何内容检查此随机数。 3) 如果不存在,请将其添加到 4) 使用此号码查找“每日单词”。 5) 重复这些步骤 如果随机数确实存在,那么只需生成另一个。然后每天重复这些步骤,直到“已使用的数字”数组的大小与“当天的单词”数组的长度匹配为

我想每天生成一个唯一的随机索引来显示列表中N个单词的“每日单词”。
在每个单词都从列表中索引之前,我不希望重复相同的索引。例如,我在一个列表中有N个单词;每天的索引在N天内应该是不同的。

以下是一些开始的步骤:

1) 生成一个随机数
2) 对照数组/哈希映射/列表/任何内容检查此随机数。
3) 如果不存在,请将其添加到
4) 使用此号码查找“每日单词”。
5) 重复这些步骤

如果随机数确实存在,那么只需生成另一个。然后每天重复这些步骤,直到“已使用的数字”数组的大小与“当天的单词”数组的长度匹配为止。然而,这个过程不是很有效,我也不一定使用它,它只是让你思考一下

一些可能更好的想法:

如果你不希望它是相同的,那么就不要“随机生成”一个数字。为什么不一开始就迭代一个数组并每天增加它


您也可以生成一个随机数,找到当天的单词,然后将其从随机单词列表中删除,并重复此过程,直到列表为空,确保每次更改随机数的边界。然后,当它为空时,只需重新填充即可。

以下是一些开始步骤:

1) 生成一个随机数
2) 对照数组/哈希映射/列表/任何内容检查此随机数。
3) 如果不存在,请将其添加到
4) 使用此号码查找“每日单词”。
5) 重复这些步骤

如果随机数确实存在,那么只需生成另一个。然后每天重复这些步骤,直到“已使用的数字”数组的大小与“当天的单词”数组的长度匹配为止。然而,这个过程不是很有效,我也不一定使用它,它只是让你思考一下

一些可能更好的想法:

如果你不希望它是相同的,那么就不要“随机生成”一个数字。为什么不一开始就迭代一个数组并每天增加它


您也可以生成一个随机数,找到当天的单词,然后将其从随机单词列表中删除,并重复此过程,直到列表为空,确保每次更改随机数的边界。然后,当它为空时,只需重新填充它。

如果你真的不想记录你已经使用过的数字,你可以使用一种相当不错的机制,称为or
LFSR
。这将生成一个随机(但可预测,如果您知道这是一个LFSR)的数字序列,该序列跨越所有
n
位的数字

只需选择
n
大于您的“n”,然后扔掉任何太大的数字

/**
 * Linear feedback shift register
 *
 * Taps can be found at: See http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf See http://mathoverflow.net/questions/46961/how-are-taps-proven-to-work-for-lfsrs/46983#46983 See
 * http://www.newwaveinstruments.com/resources/articles/m_sequence_linear_feedback_shift_register_lfsr.htm See http://www.yikes.com/~ptolemy/lfsr_web/index.htm See
 * http://seanerikoconnor.freeservers.com/Mathematics/AbstractAlgebra/PrimitivePolynomials/overview.html
 *
 * @author OldCurmudgeon
 */
public class LFSR implements Iterable<BigInteger> {

    // Bit pattern for taps.
    private final BigInteger taps;
    // Where to start (and end).
    private final BigInteger start;

    // The poly must be primitive to span the full sequence.
    public LFSR(BigInteger primitivePoly, BigInteger start) {
        // Where to start from (and stop).
        this.start = start.equals(BigInteger.ZERO) ? BigInteger.ONE : start;
        // Knock off the 2^0 coefficient of the polynomial for the TAP.
        this.taps = primitivePoly.shiftRight(1);
    }

    @Override
    public Iterator<BigInteger> iterator() {
        return new LFSRIterator(start);
    }

    private class LFSRIterator implements Iterator<BigInteger> {
        // The last one we returned.

        private BigInteger last = null;
        // The next one to return.
        private BigInteger next = null;

        public LFSRIterator(BigInteger start) {
            // Do not return the seed.
            last = start;
        }

        @Override
        public boolean hasNext() {
            if (next == null) {
                /*
                 * Uses the Galois form.
                 *
                 * Shift last right one.
                 *
                 * If the bit shifted out was a 1 - xor with the tap mask.
                 */
                boolean shiftedOutA1 = last.testBit(0);
                // Shift right.
                next = last.shiftRight(1);
                if (shiftedOutA1) {
                    // Tap!
                    next = next.xor(taps);
                }
                // Never give them `start` again.
                if (next.equals(start)) {
                    // Could set a finished flag here too.
                    next = null;
                }
            }
            return next != null;
        }

        @Override
        public BigInteger next() {
            // Remember this one.
            last = hasNext() ? next : null;
            // Don't deliver it again.
            next = null;
            return last;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported.");
        }

        @Override
        public String toString() {
            return LFSR.this.toString()
                    + "[" + (last != null ? last.toString(16) : "")
                    + "-" + (next != null ? next.toString(16) : "") + "]";
        }
    }

    @Override
    public String toString() {
        return "(" + taps.toString(32) + ")-" + start.toString(32);
    }

    public static void main(String args[]) {
        try {
            new LFSRTest().test();
        } catch (Throwable t) {
            t.printStackTrace(System.err);
        }
    }
}

class LFSRTest {

    public void test(int[] tap, int base) {
        System.out.println("Test: " + Arrays.toString(tap));
        // Build the BigInteger.
        BigInteger primitive = BigInteger.ZERO;
        for (int bit : tap) {
            primitive = primitive.or(BigInteger.ONE.shiftLeft(bit));
        }
        // Stop at 100.
        int count = 100;
        LFSR lfsr = new LFSR(primitive, BigInteger.ONE);
        for (BigInteger b : lfsr) {
            if (count-- > 0) {
                System.out.println(b.toString(base));
            } else {
                break;
            }

        }
    }

    public void test() {
        // Just 6 bits.
        int[] tap7 = {6, 5, 0};
        test(tap7, 10);
        // An example 48-bit tap.
        int[] tap48 = {48, 46, 45, 44, 42, 40, 36, 34, 33, 32, 29, 27, 26, 20, 17, 16, 12, 11, 10, 5, 3, 1, 0};
        test(tap48, 32);
    }
}
/**
*线性反馈移位寄存器
*
*可在以下位置找到水龙头:请参阅http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf 看见http://mathoverflow.net/questions/46961/how-are-taps-proven-to-work-for-lfsrs/46983#46983 看见
* http://www.newwaveinstruments.com/resources/articles/m_sequence_linear_feedback_shift_register_lfsr.htm 看见http://www.yikes.com/~ptolemy/lfsr_web/index.htm参见
* http://seanerikoconnor.freeservers.com/Mathematics/AbstractAlgebra/PrimitivePolynomials/overview.html
*
*@author OldCurmudgeon
*/
公共类LFSR实现了Iterable{
//抽头的位模式。
私人最终大整数抽头;
//从哪里开始(和结束)。
私有最终大整数启动;
//多边形必须是基本体才能跨越整个序列。
公共LFSR(BigInteger基元多边形,BigInteger起始){
//从何处开始(和停止)。
this.start=start.equals(biginger.ZERO)?biginger.ONE:start;
//去掉抽头多项式的2^0系数。
this.taps=primitivePoly.shiftRight(1);
}
@凌驾
公共迭代器迭代器(){
返回新的LFSRIterator(start);
}
私有类LFSRIterator实现了迭代器{
//我们最后一次回来。
private BigInteger last=null;
//下一个回来。
private biginger next=null;
公共LFSRIterator(BigInteger开始){
//不要退回种子。
最后=开始;
}
@凌驾
公共布尔hasNext(){
if(next==null){
/*
*使用伽罗瓦形式。
*
*把最后一个右移。
*
*如果移出的位是带抽头掩码的1-xor。
*/
布尔值shiftedOutA1=最后一个.testBit(0);
//右移。
next=last.shiftRight(1);
如果(移位为A1){
//轻敲!
next=next.xor(taps);
}
//永远不要再给他们“开始”。
if(next.equals(start)){
//也可以在这里设置完成的标志。
next=null;
}
}
返回下一步!=null;
}
@凌驾
公共biginger next(){
//记住这个。
last=hasNext()?next:null;
//不要再发了。
next=null;
最后返回;
}
@凌驾
公共空间删除(){
抛出新的UnsupportedOperationException(“不受支持”);
}
@凌驾
公共字符串toString(){
返回LFSR.this.toString()
+“[”+(last!=null?last.toString(16):“”)
+“-”+(next!=null?next.toString(16):“)+”];
}
}
@凌驾
公共字符串toString(){
返回“(”+taps.toString(