Skip to content

Instantly share code, notes, and snippets.

@MerlinTwi
Last active September 5, 2017 22:46
Show Gist options
  • Save MerlinTwi/bc986dec4f6f872ebc2c15752b4bf677 to your computer and use it in GitHub Desktop.
Save MerlinTwi/bc986dec4f6f872ebc2c15752b4bf677 to your computer and use it in GitHub Desktop.
Псевдорандомный перебор всех чисел от 1..x
// Псевдорандомно перебирает все числа (кроме 0) без повторов
// https://ru.wikipedia.org/wiki/Регистр_сдвига_с_линейной_обратной_связью
uint feed = 0x12; // для результата в 5 бит
uint i = 1;
do {
if ((i & 1) != 0)
i = (i >> 1) ^ feed;
else
i = (i >> 1);
Console.Write(" "+i); // как-то используем полученное число i
} while (i != 1);
// Output: 18 9 22 11 23 25 30 15 21 24 12 6 3 19 27 31 29 28 14 7 17 26 13 20 10 5 16 8 4 2 1
/*
https://users.ece.cmu.edu/~koopman/lfsr/index.html
биты, диапазон, варианты feed
3 [1..7] - 6
4 [1..0xF] - 0x9, 0xC
5 [1..0x1F] - 0x12, 0x14, 0x1E
6 [1..0x3F] - 0x21, 0x2D, 0x39
7 [1..0x7F] - 0x41, 0x44, 0x53
8 [1..0xFF] - 0x8E, 0x95, 0xFA
9 [1..0x1FF] - 0x108, 0x10D, 0x1FD
10 [1..0x3FF] - 0x204, 0x216, 0x3FC
11 [1..0x7FF] - 0x402, 0x40B, 0x64B
12 [1..0xFFF] - 0x829, 0x834, 0xD8F
13 [1..0x1FFF] - 0x100D, 0x1013, 0x1296
14 [1..0x3FFF] - 0x2015, 0x201C, 0x2496
15 [1..0x7FFF] - 0x4001, 0x4008, 0x4357
16 [1..0xFFFF] - 0x8016, 0x801C, 0x8679
...
20 [1..0xFFFFF] - 0x80004, 0x8033D, 0x807B8
24 [1..0xFFFFFF] - 0x80000D, 0x800422, 0x800B87
28 [1..0xFFFFFFF] - 0x8000004, 0x800016C, 0x80009E3
32 [1..0xFFFFFFFF] - 0x80000057, 0x800004F3, 0x80000EA6
64 [1..0xFFFFFFFFFFFFFFFF] - 0x800000000000000D, 0x8000000000000D0F, 0x80000000000019E2 (uint заменить на ulong)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment