Creating random barcodes is a design issue

I am fixing errors (problems with threads) in java code that generate barcodes. By design, the barcode is simply a number, and the next barcode is "not used", the next in sequence. There are 99 billion possible numbers.

First of all, I don't like automatic extensions because of security concerns. I want to generate random numbers.

Already used barcodes are stored in the database table.

It is very simple to create a random number and check the table if a barcode is used. But the logic will have to go in cycles until it finds a random number that is not used. Over time, this can be a daunting task.

I think a secure cache with 1000 free barcodes would do the job, but creating or updating a cache can be quite difficult.

Any suggestions for a query design that does a search, or a query that can return a range of free random numbers?

I use hibernation criteria.

thanks

+4
java multithreading database
source share
1 answer

You can use the Linear Feedback Shift Register . They can go through all numbers with a certain number of bits in a loop without repeating, but in a seemingly random order.

This should work - this is a very shortened version of the one I wrote some time ago. You will need to select your primitive polynomial, as described in the links.

You can find a list of randomly selected primitive polynomials here .

/** * 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 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, 16); } 
+4
source share

All Articles