8 Digit Random Number

0 views
Skip to first unread message

Cesar Sergeantson

unread,
Aug 3, 2024, 2:38:48 PM8/3/24
to sigsuhasi

You tell that you check for duplicates, but be cautious since when most numbers will be used, the number of "attempts" (and therefore the time taken) for getting a new number will increase, possibly resulting in very long delays & wasting CPU resources.

There are some great answers, but many use functions that are flagged as not cryptographically secure. If you want a random 6 digit number that is cryptographically secure you can use something like this:

Caution This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.

Second time through the loopGenerate a random number between 100,000 and x1 call this xt2, then generate a random number between x1 and 999,999 call this xt3, then randomly choose x2 or x3, call this x2

Note that here we use random_int, which was introduced in PHP 7 and uses a cryptographic random generator, something that is important if you want random codes to be hard to guess. random_bytes was also introduced in PHP 7 and likewise uses a cryptographic random generator.

The code above generates a string of 6 decimal digits. If you want to use a bigger character set (such as all upper-case letters, all lower-case letters, and the 10 digits), this is a more involved process, but you have to use random_int or random_bytes rather than rand(), mt_rand(), str_shuffle(), etc., if the string will serve as a password, a "confirmation code", or another secret value. See an answer to a related question, and see also: generating a random code in php?

A pseudo-random number generator (PRNG) is typically programmed using a randomizing math function to select a "random" number within a set range. These random number generators are pseudo-random because the computer program or algorithm may have unintended selection bias. In other words, randomness from a computer program is not necessarily an organic, truly random event.

A true random number generator (TRNG) relies on randomness from a physical event that is external to the computer and its operating system. Examples of such events are blips in atmospheric noise, or points at which a radioactive material decays. A true random number generator receives information from these types of unpredictable events to produce a truly random number.

A random number is a number chosen from a pool of limited or unlimited numbers that has no discernible pattern for prediction. The pool of numbers is almost always independent from each other. However, the pool of numbers may follow a specific distribution. For example, the height of the students in a school tends to follow a normal distribution around the median height. If the height of a student is picked at random, the picked number has a higher chance to be closer to the median height than being classified as very tall or very short. The random number generators above assume that the numbers generated are independent of each other, and will be evenly spread across the whole range of possible values.

A random number generator, like the ones above, is a device that can generate one or many random numbers within a defined scope. Random number generators can be hardware based or pseudo-random number generators. Hardware based random-number generators can involve the use of a dice, a coin for flipping, or many other devices.

A pseudo-random number generator is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random numbers. Computer based random number generators are almost always pseudo-random number generators. Yet, the numbers generated by pseudo-random number generators are not truly random. Likewise, our generators above are also pseudo-random number generators. The random numbers generated are sufficient for most applications yet they should not be used for cryptographic purposes. True random numbers are based on physical phenomena such as atmospheric noise, thermal noise, and other quantum phenomena. Methods that generate true random numbers also involve compensating for potential biases caused by the measurement process.

If you can tell us the entire story of what you want we don't have to keep coming back with more questions. So far we know you want a 7 digit random that must be unique. Do you have a table this belong in or this a separate table of these values? Do you need to generate these one at a time (like for an insert) or do you need a big list of them? The reason is because depending on the needs it will greatly affect how to go about this.

There are exactly 8,999,999 distinct integers between 1,000,000 and 9,999,999. Depending on what these numbers are being generated for, you could start having collisions reasonably soon (thanks to our good friend the birthday paradox). If the only criteria is length and uniqueness, and identity column starting at 1000000 with a check constraint limiting it to less than 10,000,000 would constraint it such.

I need to create a 4 digit random number, however when I used new random().Next(0000,9999) this also generated results such as 123/ 25. I need it to start with 0123 or 0025 - to get 4 digits. Any idea how to do this? I tried formatvalue but that does not seem right.

Part of what I do is study typical behavior of large combinatorial structures by looking at pseudorandom instances. But many commercially available pseudorandom number generators have known defects, which makes me wonder whether I should just use the digits (or bits) of $\pi$.

A colleague of mine says he "read somewhere" that the digits of $\pi$ don't make a good random number generator. Perhaps he's thinking of the article "A study on the randomness of the digits of $\pi$" by Shu-Ju Tu and Ephraim Fischbach. Does anyone know this article? Some of the press it got (see e.g. ) made it sound like $\pi$ wasn't such a good source of randomness, but the abstract for the article itself (see ) suggests the opposite.

If you are worried about the quality of random digits that you're getting, then you may want to use cryptographic random number generators. For example, finding a pattern in the Blum-Blum-Shub random number generator would probably yield a new algorithm for factoring large integers! Cryptographic random number generators will run more slowly than the "commercial" random number generators you're talking about but you can certainly find some that will generate digits faster than algorithms for computing $\pi$ will.

In a technical sense, no. A good pseudorandom number generator would be one that you can plug into any randomized algorithm and expect to see the same behavior that you would from an actual random number generator. One way of making a technical definition out of this is to say that the pseudorandom number generator cannot be distinguished from truly random (with probability bounded away from 1/2) by any polynomial time test.

For the same reason, no fully deterministic sequence can be a good random sequence. Instead, to fit this definition, you need to use a pseudorandom number generator that takes some number n of truly random bits as an input seed and generates from them a longer sequence (polynomial in n) of pseudorandom bits that cannot be distinguished from random by a polynomial time algorithm.

I'd say no if you are using the random numbers to generate cryptographic keys, then you immediately open yourself to attacks, because the attacker can probably mimic your random number generator, and thus you add one weak link into the chain.

But are the digits "as good as random"? The short answer is that, as far as anyone can tell, the answer seems to be empirically yes (See Marsaglia's On the Randomness of Pi and Other Decimal Expansions). That is, there are no known ways in which the digits of $\pi$ behave systematically unlike a random number.

There are several other answers here that I think are confusing on this point. Let me explain why some of the claims made by other answers (despite being technically correct and in certain ways very insightful) don't disprove the claim that the digits of $\pi$ are as good as random.

Cryptographic PRNG's are the gold standard since if you have a practical way to detect the slightest non-randomness in the output, that is considered a break against the generator, and a significant research result (in cryptanalysis) if the PRNG was considered any good (say if it was based on AES, the Advanced Encryption Standard, in some sensible way). It's easy to make them deterministic: for any key K, just take the encryptions E(0), E(1), E(2), ... where E is the encryption function.

Recent x86 computers have a hardware instruction for AES encryption, so it is very fast. It wouldn't surprise me if AES using the hardware instruction is faster than Mersenne Twister implemented in software.

Given all digits of a sequence S till a certain length say n ie di ( i = 1 to n) ; if the probability of any next block of digits B in next m digits ( m -> infinity ) can be ascertained as < 1/(b^w) where b is the base and w is the string length of the block , through an algorithm which is guaranteed to halt then S is NOT a random sequence.

Direction of analysis is also important. Suppose there is a civilization where constant Pi has not been discovered yet ( let alone its formula), here a only a reverse analysis would be possible and the probability of one chancing upon the spigot formula while analysing the digits of Pi cannot be ruled out though its remote. Other wise the equidistribution of digits would lead such a civilisation to take Pi sequence as random

Hi there, I am new to programming and i'm looking to generate a random 10 digit number between "1000000000 and 2147483647" which i can do with the "random()" function but i would like to send it via an NRF module which uses "uint8_t and char".

The above codes work and "TEXT" is sent without issues.
I would like to generate a random 6-10 digit number which would replace "TEXT".
I have tried using different data types like "long" along with "random()" but the data gets corrupted.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages