Get random int
Get random int means ask a tool or a function for one whole number in a range. You receive an integer. You do not receive a fraction, a name, or a certificate that this was a public lottery.
How it works
The call has three parts even when a page hides them: a lower bound, an upper bound, and a source of unpredictability. The function maps the source onto the integers that sit between those bounds. If the mapping is sloppy, some integers show up more often than others. If the bounds are sloppy, you get a number you did not mean, which is a logic error, not bad luck.
On a web page, “get” is a button. In code, “get” is a name people type because they want a value back, not a lecture. The job is the same. Read whether the top is included. Languages disagree. Some random-integer helpers include both ends. Some include the start and exclude the end. Copying a snippet from one language into another without reading that rule is a classic off-by-one.
A cryptographic source, when the platform offers one, is the right default for a token, a shuffle of secret IDs, or anything an attacker would like to predict. A fast, seedable recipe is the right default for a replayable simulation. Do not mix them. A seeded recipe you can replay is a feature in a test and a hole in a raffle.
The off-by-one that eats the last int
Suppose you wanted 1 through 6. A helper that takes a count of faces and starts at zero will give 0 through 5 unless you add one. A helper that takes a minimum and a maximum must say whether the maximum is legal. Write a tiny table for the first and last values you actually received. If 6 never arrives in a long session, the helper is probably exclusive at the top, or you passed a width of 5.
Inclusive interval: both ends can appear. Half-open interval: the top is a fence, not a face. Zero-based width: n faces often means 0 through n−1, then you shift. Safe integer limits: enormous ranges can overflow a language’s idea of a whole number.
Getting several ints is a loop or a batch. If you need unique values, do not loop and hope. Draw a shuffle of the interval, or pick without putting the value back. A loop that retries on collision is fine for a tiny set and a small take. It is a stall if you ask for too many unique values from a tiny interval.
Button, library, or hardware
A page button is for a person who needs a number now. A library call is for software that needs a number inside a program. Hardware noise and dedicated services exist for groups that want a source they can all look at and that is not a seed in someone’s pocket. None of those three is “more random” in the abstract. They are different claims. Match the claim to the stakes, then record the range with the result.
Getting an int is easy. Getting the int you meant, with the end you meant, is the actual work. Print the bounds in logs when the draw decides something a user will see. Silent randomness is how “it picked 0” becomes an afternoon of debugging. The int is the answer. The interval is the question. Store both.
Questions and answers
- Does the helper include the maximum?
- Only if that helper says so. Read the inclusive or exclusive rule. Do not assume the page and the language agree.
- Why did I get 0 when I wanted 1–6?
- The helper likely starts at zero or treats the top as exclusive. Shift the range, or pass the bounds you actually meant.
- Can I replay the same int later?
- Yes if the source is seeded and you kept the seed. No if the source is a one-way cryptographic draw. Choose that on purpose.
Related guides
Address random generator
An address random generator builds a fake street line so you do not invent one by hand. Searches for random address generator, address generator random, and a New York variant still want a sample, not
Alphabet random generator
An alphabet random generator shuffles the letters of an alphabet into a new order. For English that is a permutation of A through Z, each letter once.
Animal generator random
An animal generator random pick is one animal name drawn from the tool’s list. Each click returns one animal from that list, not a living creature and not a complete catalog of Earth.