David Velasquez
David Velasquez

Reputation: 2366

Is there a way I can get a random number from 0 - 1 billion?

I'm not caring for efficiency right now. I'm just looking for a decent way I can generate random numbers from 0 through 1 billion. I've tried doing rand() * rand() but it's been giving me only numbers estimating greater than about 10 million. I would like the range to be way more spread out. Anyone have any suggestions?

Upvotes: 3

Views: 1760

Answers (3)

Baum mit Augen
Baum mit Augen

Reputation: 50063

Sure, just use the modern <random> facilities of C++:

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 OP needs) will do what you need.

An analog function for floating point values also exists if needed.


Remark: In the unlikely case that your platform's int cannot hold one billion, or if you need even bigger numbers, you can also use bigger integer types like this:

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

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

Upvotes: 11

Uday Yadav
Uday Yadav

Reputation: 11

RANDOMIZED SERIAL/SEQUENTIAL NUMBERS (UNIQUE & UNPREDICTABLE)

If only the random numbers are allowed to be of unique value.

12345678900 72 12345678901 34. 12345678926 34. 12345678951 24. 12345678976 84. 12345678902 65. 12345678927 63. 12345678952 51.
12345678977 67. 12345678903 09. 12345678928 11. 12345678953 19.
12345678978 53. 12345678904 22. 12345678929 44. 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. 12345678983 77. 12345678909 71. 12345678934 49. 12345678959 83. 12345678984 13. 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 10. 12345678938 48. 12345678963 94. 12345678988 40. 12345678914 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. 12345678942 14. 12345678967 47. 12345678992 65. 12345678918 52. 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. 12345678922 26. 12345678947 69. 12345678972 43. 12345678997 29. 12345678923 91. 12345678948 82. 12345678973 59. 12345678998 33. 12345678924 05. 12345678949 56. 12345678974 81. 12345678999 78. 12345678925 36. 12345678950 68. 12345678975 90. 12345679000 06.

These are 101 unique random numbers.

Each number consists of 13 digits, out of which first 11 digits are sequential numbers and the 12th and 13th digits together form a random number. These last two digits transform the 11 digit sequential number into a 13 digit random number. Thus when a sequential number is transformed into a random number by addition of 1 or 2 digits, such randomization does not need math based algorithm.

Even if the two digits are created by math based algorithms, there can be innumerable such algorithms that can create two digit random numbers.

Hence, my claim is that when 1, 2 or 3 randomly created digits are attached to the sequential number, you award randomness to it and such randomized sequential numbers are unpredictable.

Thus a SHORTEST POSSIBLE sequence of 11 digits can accommodate one billion unpredictable random numbers and a sequence of only 14 digits can accommodate one trillion unpredictable random numbers.

Upvotes: 1

rossum
rossum

Reputation: 15685

One billion is just below 2^30. If you can't generate a 30 bit number directly, then generate two 15-bit numbers, shift one left by 15 bits and XOR with the unshifted number to get a 30-bit number.

If the 30-bit result exceeds 1 billion, then throw it away and generate another 30-bit number. 2^30 = 1073741824, so the result will only be too large in about 7% of cases.

Upvotes: 1

Related Questions