views:

102

answers:

3

Hi, I am unsure how to put this and my math skills aren't that strong. But here's what I need.

I want to generate a list of all the 16bit integers (0-65535). But everytime I do so I want to seed the algorithm randomly that each time the list starts with a different integer and all the subsequent integers will be generated once but also in random order.

small example (1-5): ...

1, 5, 3, 2, 4

4, 3, 1, 5, 2

2, 1, 4, 5, 3 ...

Any help?

+4  A: 

The easiest way is probably to generate the list of required numbers and then shuffle it.

LukeH
That works, but what if I need to generate a list of 2^31 integers? Is there an algorithm thinkable with which I can only retain the state of the initial seed + the last generated number and then just call get_next_number() on it ? That would be way faster.
vvv
[code]unsigned int random_seed = 0;unsigned int sample_size = 3000;unsigned int generated_number = random_seed % sample_size;unsigned int prime_number = 7;unsigned int increment = prime_number;for(unsigned int iterator = 0; iterator < sample_size; ++iterator){ generated_number = (generated_number + increment) % sample_size;}http://en.wikipedia.org/wiki/Full_cycle[/code]
vvv
I think you might be able to do that using a Linear Feedback Shift Register. I guess you'd need to use a 32-bit LFSR and ensure that it was setup correctly to return the max length sequence. (Disclaimer: I know nothing about this topic, so I could be completely wrong!)
LukeH
See http://en.wikipedia.org/wiki/Linear_feedback_shift_register and http://en.wikipedia.org/wiki/Maximum_length_sequence for more info.
LukeH
+1  A: 
  1. Create a list of integers. In your example from 1 to 5 inclusive;
  2. Perform a correct shuffle on it. For example, the Fisher-Yates shuffle. The "correct" part is important as an incorrect shuffle algorithm will yield biased results.

You don't say what language you use but here are two:

PHP

$arr = range(1, 5);
shuffle($arr);

Java

List<Integer> list = new ArrayList<Integer>();
for (int i=1; i<=5; i++) {
  list.add(i);
}
Collections.shuffle(list);
cletus
+3  A: 

Assuming you have no security requirements, possibly the easiest method is to create a Linear Congruential Generator (LCG) and select a new multiplier and/or increment every time you need to change the sequence. Using the notation from the Wikipedia entry, if you want all k-bit integers, your modulus is m=2k and conditions 1, 2, and 3 simplify to:
1. c is odd
2. a = 1 + 4*x for any integer x

There are other generators that are similar to LCG that should be just as satisfactory.

GregS
+1000 why is this answer not at top?
BlueRaja - Danny Pflughoeft