[{"data":1,"prerenderedAt":864},["ShallowReactive",2],{"guide-random-number-guide":3},{"id":4,"title":5,"body":6,"date":855,"description":856,"extension":857,"meta":858,"navigation":859,"path":860,"readingTime":648,"seo":861,"stem":862,"__hash__":863},"guides\u002Fguides\u002Frandom-number-guide.md","Random Number Guide",{"type":7,"value":8,"toc":845},"minimark",[9,14,32,35,39,127,130,134,137,150,196,204,272,281,285,288,293,356,365,428,438,461,465,468,490,676,679,683,778,787,791,828,832,841],[10,11,13],"h2",{"id":12},"what-is-randomness-in-computing","What Is Randomness in Computing?",[15,16,17,18,22,23,27,28,31],"p",{},"Randomness in computing means producing values that lack any predictable pattern. Computers are deterministic machines — they follow instructions exactly — so generating true randomness is fundamentally hard. Instead, most programming languages rely on ",[19,20,21],"strong",{},"pseudo-random number generators (PRNGs)",": algorithms that produce sequences which ",[24,25,26],"em",{},"look"," random but are actually determined by an initial value called a ",[19,29,30],{},"seed",".",[15,33,34],{},"True randomness requires an external source of entropy, such as hardware noise, radioactive decay, or user input timing. Most everyday applications do not need this level of unpredictability and work fine with PRNGs.",[10,36,38],{"id":37},"pseudo-random-vs-true-random","Pseudo-Random vs True Random",[40,41,42,58],"table",{},[43,44,45],"thead",{},[46,47,48,52,55],"tr",{},[49,50,51],"th",{},"Aspect",[49,53,54],{},"Pseudo-Random",[49,56,57],{},"True Random",[59,60,61,75,88,101,114],"tbody",{},[46,62,63,69,72],{},[64,65,66],"td",{},[19,67,68],{},"Source",[64,70,71],{},"Mathematical algorithm",[64,73,74],{},"Physical entropy",[46,76,77,82,85],{},[64,78,79],{},[19,80,81],{},"Reproducible",[64,83,84],{},"Yes, with same seed",[64,86,87],{},"No",[46,89,90,95,98],{},[64,91,92],{},[19,93,94],{},"Speed",[64,96,97],{},"Fast",[64,99,100],{},"Slower",[46,102,103,108,111],{},[64,104,105],{},[19,106,107],{},"Predictability",[64,109,110],{},"Predictable if seed known",[64,112,113],{},"Unpredictable",[46,115,116,121,124],{},[64,117,118],{},[19,119,120],{},"Use case",[64,122,123],{},"Games, simulations, testing",[64,125,126],{},"Cryptography, security tokens",[15,128,129],{},"A PRNG always produces the same output for the same seed. This is useful for debugging and reproducible tests, but it means anyone who knows the seed can predict every number in the sequence.",[10,131,133],{"id":132},"mathrandom-vs-cryptogetrandomvalues","Math.random() vs crypto.getRandomValues()",[15,135,136],{},"In JavaScript, the two most common ways to generate random numbers serve very different purposes.",[15,138,139,145,146,149],{},[19,140,141],{},[142,143,144],"code",{},"Math.random()"," returns a floating-point number between 0 (inclusive) and 1 (exclusive). It uses a PRNG internally and is fast, but it is ",[19,147,148],{},"not cryptographically secure",". An attacker who observes enough outputs can predict future values.",[151,152,157],"pre",{"className":153,"code":154,"language":155,"meta":156,"style":156},"language-javascript shiki shiki-themes github-light github-dark","\u002F\u002F Simple PRNG — fast but not secure\nconst value = Math.random(); \u002F\u002F 0.374527...\n","javascript","",[142,158,159,168],{"__ignoreMap":156},[160,161,164],"span",{"class":162,"line":163},"line",1,[160,165,167],{"class":166},"sJ8bj","\u002F\u002F Simple PRNG — fast but not secure\n",[160,169,171,175,179,182,186,190,193],{"class":162,"line":170},2,[160,172,174],{"class":173},"szBVR","const",[160,176,178],{"class":177},"sj4cs"," value",[160,180,181],{"class":173}," =",[160,183,185],{"class":184},"sVt8B"," Math.",[160,187,189],{"class":188},"sScJk","random",[160,191,192],{"class":184},"(); ",[160,194,195],{"class":166},"\u002F\u002F 0.374527...\n",[15,197,198,203],{},[19,199,200],{},[142,201,202],{},"crypto.getRandomValues()"," fills a typed array with cryptographically strong random values. It draws from the operating system's entropy pool, making it suitable for security-critical tasks.",[151,205,207],{"className":153,"code":206,"language":155,"meta":156,"style":156},"\u002F\u002F Cryptographically secure\nconst array = new Uint32Array(1);\ncrypto.getRandomValues(array);\nconst secureInt = array[0]; \u002F\u002F 0 to 4,294,967,295\n",[142,208,209,214,238,250],{"__ignoreMap":156},[160,210,211],{"class":162,"line":163},[160,212,213],{"class":166},"\u002F\u002F Cryptographically secure\n",[160,215,216,218,221,223,226,229,232,235],{"class":162,"line":170},[160,217,174],{"class":173},[160,219,220],{"class":177}," array",[160,222,181],{"class":173},[160,224,225],{"class":173}," new",[160,227,228],{"class":188}," Uint32Array",[160,230,231],{"class":184},"(",[160,233,234],{"class":177},"1",[160,236,237],{"class":184},");\n",[160,239,241,244,247],{"class":162,"line":240},3,[160,242,243],{"class":184},"crypto.",[160,245,246],{"class":188},"getRandomValues",[160,248,249],{"class":184},"(array);\n",[160,251,253,255,258,260,263,266,269],{"class":162,"line":252},4,[160,254,174],{"class":173},[160,256,257],{"class":177}," secureInt",[160,259,181],{"class":173},[160,261,262],{"class":184}," array[",[160,264,265],{"class":177},"0",[160,267,268],{"class":184},"]; ",[160,270,271],{"class":166},"\u002F\u002F 0 to 4,294,967,295\n",[15,273,274,275,277,278,280],{},"Use ",[142,276,144],{}," for games, animations, and non-security use cases. Use ",[142,279,202],{}," for tokens, passwords, and any value an attacker must not predict.",[10,282,284],{"id":283},"integer-vs-decimal-generation","Integer vs Decimal Generation",[15,286,287],{},"Most random number generators produce decimal (floating-point) values between 0 and 1. You often need to convert these into integers within a specific range.",[15,289,290],{},[19,291,292],{},"Decimal in a range [min, max):",[151,294,296],{"className":153,"code":295,"language":155,"meta":156,"style":156},"function randomDecimal(min, max) {\n  return Math.random() * (max - min) + min;\n}\n",[142,297,298,321,351],{"__ignoreMap":156},[160,299,300,303,306,308,312,315,318],{"class":162,"line":163},[160,301,302],{"class":173},"function",[160,304,305],{"class":188}," randomDecimal",[160,307,231],{"class":184},[160,309,311],{"class":310},"s4XuR","min",[160,313,314],{"class":184},", ",[160,316,317],{"class":310},"max",[160,319,320],{"class":184},") {\n",[160,322,323,326,328,330,333,336,339,342,345,348],{"class":162,"line":170},[160,324,325],{"class":173},"  return",[160,327,185],{"class":184},[160,329,189],{"class":188},[160,331,332],{"class":184},"() ",[160,334,335],{"class":173},"*",[160,337,338],{"class":184}," (max ",[160,340,341],{"class":173},"-",[160,343,344],{"class":184}," min) ",[160,346,347],{"class":173},"+",[160,349,350],{"class":184}," min;\n",[160,352,353],{"class":162,"line":240},[160,354,355],{"class":184},"}\n",[15,357,358],{},[19,359,360,361,364],{},"Integer in a range ",[160,362,363],{},"min, max"," (inclusive):",[151,366,368],{"className":153,"code":367,"language":155,"meta":156,"style":156},"function randomInteger(min, max) {\n  return Math.floor(Math.random() * (max - min + 1)) + min;\n}\n",[142,369,370,387,424],{"__ignoreMap":156},[160,371,372,374,377,379,381,383,385],{"class":162,"line":163},[160,373,302],{"class":173},[160,375,376],{"class":188}," randomInteger",[160,378,231],{"class":184},[160,380,311],{"class":310},[160,382,314],{"class":184},[160,384,317],{"class":310},[160,386,320],{"class":184},[160,388,389,391,393,396,399,401,403,405,407,409,412,414,417,420,422],{"class":162,"line":170},[160,390,325],{"class":173},[160,392,185],{"class":184},[160,394,395],{"class":188},"floor",[160,397,398],{"class":184},"(Math.",[160,400,189],{"class":188},[160,402,332],{"class":184},[160,404,335],{"class":173},[160,406,338],{"class":184},[160,408,341],{"class":173},[160,410,411],{"class":184}," min ",[160,413,347],{"class":173},[160,415,416],{"class":177}," 1",[160,418,419],{"class":184},")) ",[160,421,347],{"class":173},[160,423,350],{"class":184},[160,425,426],{"class":162,"line":240},[160,427,355],{"class":184},[15,429,430,431,434,435,437],{},"The ",[142,432,433],{},"+1"," in the integer formula ensures the upper bound is included. Without it, ",[142,436,317],{}," would never appear in the output.",[15,439,440,443,444,447,448,451,452,454,455,457,458,460],{},[19,441,442],{},"Common mistake:"," Using ",[142,445,446],{},"Math.round()"," instead of ",[142,449,450],{},"Math.floor()"," biases the distribution — the endpoints ",[142,453,311],{}," and ",[142,456,317],{}," each appear half as often as interior values. Always use ",[142,459,450],{}," for uniform integer generation.",[10,462,464],{"id":463},"generating-unique-random-values","Generating Unique Random Values",[15,466,467],{},"Sometimes you need random values that do not repeat within a set. Common approaches include:",[469,470,471,478,484],"ul",{},[472,473,474,477],"li",{},[19,475,476],{},"Shuffle and pick:"," Generate all possible values, shuffle the list, then take what you need. Works well when the range is small.",[472,479,480,483],{},[19,481,482],{},"Track used values:"," Keep a set of previously generated values and reject duplicates. Simple but degrades in performance as the pool fills up.",[472,485,486,489],{},[19,487,488],{},"Cryptographic uniqueness:"," Use UUID v4 for identifiers that are virtually guaranteed to be unique without coordination.",[151,491,493],{"className":153,"code":492,"language":155,"meta":156,"style":156},"\u002F\u002F Fisher-Yates shuffle for unique picks\nfunction shuffledRange(min, max) {\n  const arr = Array.from({ length: max - min + 1 }, (_, i) => i + min);\n  for (let i = arr.length - 1; i > 0; i--) {\n    const j = Math.floor(Math.random() * (i + 1));\n    [arr[i], arr[j]] = [arr[j], arr[i]];\n  }\n  return arr;\n}\n",[142,494,495,500,517,569,613,646,657,663,671],{"__ignoreMap":156},[160,496,497],{"class":162,"line":163},[160,498,499],{"class":166},"\u002F\u002F Fisher-Yates shuffle for unique picks\n",[160,501,502,504,507,509,511,513,515],{"class":162,"line":170},[160,503,302],{"class":173},[160,505,506],{"class":188}," shuffledRange",[160,508,231],{"class":184},[160,510,311],{"class":310},[160,512,314],{"class":184},[160,514,317],{"class":310},[160,516,320],{"class":184},[160,518,519,522,525,527,530,533,536,538,540,542,544,547,550,552,555,558,561,564,566],{"class":162,"line":240},[160,520,521],{"class":173},"  const",[160,523,524],{"class":177}," arr",[160,526,181],{"class":173},[160,528,529],{"class":184}," Array.",[160,531,532],{"class":188},"from",[160,534,535],{"class":184},"({ length: max ",[160,537,341],{"class":173},[160,539,411],{"class":184},[160,541,347],{"class":173},[160,543,416],{"class":177},[160,545,546],{"class":184}," }, (",[160,548,549],{"class":310},"_",[160,551,314],{"class":184},[160,553,554],{"class":310},"i",[160,556,557],{"class":184},") ",[160,559,560],{"class":173},"=>",[160,562,563],{"class":184}," i ",[160,565,347],{"class":173},[160,567,568],{"class":184}," min);\n",[160,570,571,574,577,580,582,585,588,591,594,596,599,602,605,608,611],{"class":162,"line":252},[160,572,573],{"class":173},"  for",[160,575,576],{"class":184}," (",[160,578,579],{"class":173},"let",[160,581,563],{"class":184},[160,583,584],{"class":173},"=",[160,586,587],{"class":184}," arr.",[160,589,590],{"class":177},"length",[160,592,593],{"class":173}," -",[160,595,416],{"class":177},[160,597,598],{"class":184},"; i ",[160,600,601],{"class":173},">",[160,603,604],{"class":177}," 0",[160,606,607],{"class":184},"; i",[160,609,610],{"class":173},"--",[160,612,320],{"class":184},[160,614,616,619,622,624,626,628,630,632,634,636,639,641,643],{"class":162,"line":615},5,[160,617,618],{"class":173},"    const",[160,620,621],{"class":177}," j",[160,623,181],{"class":173},[160,625,185],{"class":184},[160,627,395],{"class":188},[160,629,398],{"class":184},[160,631,189],{"class":188},[160,633,332],{"class":184},[160,635,335],{"class":173},[160,637,638],{"class":184}," (i ",[160,640,347],{"class":173},[160,642,416],{"class":177},[160,644,645],{"class":184},"));\n",[160,647,649,652,654],{"class":162,"line":648},6,[160,650,651],{"class":184},"    [arr[i], arr[j]] ",[160,653,584],{"class":173},[160,655,656],{"class":184}," [arr[j], arr[i]];\n",[160,658,660],{"class":162,"line":659},7,[160,661,662],{"class":184},"  }\n",[160,664,666,668],{"class":162,"line":665},8,[160,667,325],{"class":173},[160,669,670],{"class":184}," arr;\n",[160,672,674],{"class":162,"line":673},9,[160,675,355],{"class":184},[15,677,678],{},"Choose the method based on your range size and how many values you need relative to the total pool.",[10,680,682],{"id":681},"common-use-cases","Common Use Cases",[40,684,685,698],{},[43,686,687],{},[46,688,689,692,695],{},[49,690,691],{},"Use Case",[49,693,694],{},"Type Needed",[49,696,697],{},"Generator",[59,699,700,713,726,739,752,765],{},[46,701,702,707,710],{},[64,703,704],{},[19,705,706],{},"Lottery picks",[64,708,709],{},"Unique integers, no repeats",[64,711,712],{},"Shuffle or rejection sampling",[46,714,715,720,723],{},[64,716,717],{},[19,718,719],{},"Statistical sampling",[64,721,722],{},"Decimal or integer, uniform distribution",[64,724,725],{},"PRNG (Math.random)",[46,727,728,733,736],{},[64,729,730],{},[19,731,732],{},"Game dice\u002Fcards",[64,734,735],{},"Integer in range, possibly unique",[64,737,738],{},"PRNG with shuffle",[46,740,741,746,749],{},[64,742,743],{},[19,744,745],{},"Unit test fixtures",[64,747,748],{},"Reproducible random data",[64,750,751],{},"Seeded PRNG",[46,753,754,759,762],{},[64,755,756],{},[19,757,758],{},"Security tokens",[64,760,761],{},"Unpredictable, unique",[64,763,764],{},"CSPRNG (crypto.getRandomValues)",[46,766,767,772,775],{},[64,768,769],{},[19,770,771],{},"A\u002FB test assignment",[64,773,774],{},"Integer or boolean",[64,776,777],{},"PRNG with deterministic seed",[15,779,780,781,783,784,786],{},"Each use case has different requirements for speed, uniqueness, and unpredictability. Picking the right generator matters — using ",[142,782,144],{}," for a security token is a vulnerability, while using ",[142,785,202],{}," for a game animation is unnecessary overhead.",[10,788,790],{"id":789},"key-takeaways","Key Takeaways",[469,792,793,804,809,814,822,825],{},[472,794,795,796,799,800,803],{},"Computers generate ",[19,797,798],{},"pseudo-random"," numbers from algorithms; ",[19,801,802],{},"true random"," numbers require physical entropy sources.",[472,805,806,808],{},[142,807,144],{}," is fast but predictable — never use it for security-critical values.",[472,810,811,813],{},[142,812,202],{}," provides cryptographically strong randomness suitable for tokens and passwords.",[472,815,274,816,818,819,821],{},[142,817,450],{},", not ",[142,820,446],{},", for uniform integer distribution.",[472,823,824],{},"For unique values, shuffle-based approaches scale better than rejection sampling when you need a large fraction of the available range.",[472,826,827],{},"Match your generator to your use case: PRNG for speed, CSPRNG for security.",[10,829,831],{"id":830},"try-it-yourself","Try It Yourself",[15,833,834,835,840],{},"Generate random integers, decimals, or unique sequences instantly with our free ",[836,837,839],"a",{"href":838},"\u002Ftools\u002Frandom-number","Random Number Generator"," — no code required.",[842,843,844],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":156,"searchDepth":170,"depth":170,"links":846},[847,848,849,850,851,852,853,854],{"id":12,"depth":170,"text":13},{"id":37,"depth":170,"text":38},{"id":132,"depth":170,"text":133},{"id":283,"depth":170,"text":284},{"id":463,"depth":170,"text":464},{"id":681,"depth":170,"text":682},{"id":789,"depth":170,"text":790},{"id":830,"depth":170,"text":831},"2026-05-28","Understand how random number generators work and when to use integer, decimal, or unique random values.","md",{"immutable":859},true,"\u002Fguides\u002Frandom-number-guide",{"title":5,"description":856},"guides\u002Frandom-number-guide","GXZ8QuE2l5HlpvkegqsubckhgOniilSG8Mj21FE6a3U",1780401328999]