Is there a way to get a random number between 0 and 1 billion?

I don't care about efficiency right now. I'm just looking for a decent way to generate random numbers between 0 and 1 billion. I tried doing rand () * rand (), but that only gave me numbers valued at over 10 million. I would like the range to be more common. Anyone have any suggestions?

+3


source to share


3 answers


Of course, just use modern <random>

C ++ tools:

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 1000000000);

for (int n=0; n<10; ++n)
    std::cout << dis(gen) << ' ';
std::cout << '\n';

      

(from here , slightly modified to do what the OP requires) will do what you need.

An analogue function for floating point values ​​also exists if needed.




Note. In the unlikely event that your platform int

cannot hold one billion, or if you need even larger numbers, you can also use large integer types like this:

std::uniform_int_distribution<std::int64_t> dis(1, 1000000000);

      

Also note that the mt seeding presented here is not optimal; see my question here for more information.

+10


source


RANDOMIZED SERIAL / SEQUENCE NUMBERS (UNIQUE AND UNEXPECTED)

If only the random numbers have a unique value.

72 12345678901 12345678900 12345678926 34. 12345678951 34. 24. 84. 12345678976 12345678902 12345678927 65. 63. 51. 12345678952
12345678977 12345678903 67. 09. 11. 12345678928 12345678953 19.
12345678978 12345678904 53. 22. 44. 12345678929 12345678954 78
12345678979 04. 12345678905 21.12345678930 85.12345678955 76.
12345678980 35. 12345678906 37. 12345678931 01. 12345678956 31. 12345678981 73. 12345678907 42. 12345678932 55. 12345678957 12. 12345678982 16. 12345678908 20. 12345678933 95. 12345678958 87. 12345678838 779. 1234567 12345678910 32 12345678935 60 12345678960 50 12345678985 45 12345678911 58 12345678936 86 12345678961 02 12345678986 61 12345678912 66 12345678937 30 12345678962 64 12345678987 23 12345678913 40 12345678967 79 12345678939 89 12345678964 27 12345678989 70 12345678915 93 12345678940 43 12345678965 92 12345678990 08 12345678916 46 12345678941 72 12345678966 03 12345678991 88 12345678917 57 1234567867 65 1234567 12345678943 38 12345678968 62.
12345678993 17.12345678919 15.12345678944 75. 12345678969 80. 12345678994 54. 12345678920 41. 12345678945 07. 12345678970 18. 12345678995 28. 12345678921 62. 12345678946 25. 12345678971 58. 12345678996 74. 12345678967 . 12345678923 91 12345678948 82 12345678973 59 12345678998 33 12345678924 05 12345678949 56 12345678974 81 12345678999 78 12345678925 36 12345678950 68 12345678975 90 12345679000 06.

This is 101 unique random numbers .



Each number consists of 13 digits, of which the first 11 digits are ordinal numbers, and the 12th and 13th digits together form a random number. These last two digits convert an 11-digit sequential number to a random 13-digit number. Thus, when a sequential number is converted to a random number by adding 1 or 2 digits, such randomization does not need a mathematical algorithm.

Even if two digits are generated using mathematical algorithms, there can be many such algorithms that can generate two-digit random numbers.

Hence, my claim is that when 1, 2, or 3 randomly generated digits are appended to a sequence number, you are awarding randomness to it, and such randomized sequential numbers are unpredictable.

Thus, a SHORT POSSIBLE 11-digit sequence can hold one billion unpredictable random numbers, and a 14-digit sequence can hold one trillion unpredictable random numbers.

+1


source


One billion is just below 2 ^ 30. If you cannot generate a 30-bit number directly, then generate two 15-bit numbers, shift one 15 bits and XOR the unmoved number to get a 30-bit number.

If the 30-bit result is over 1 billion, then throw it away and create another 30-bit number. 2 ^ 30 = 1073741824, so the result will be too large about 7% of the time.

0


source







All Articles