[{"data":1,"prerenderedAt":1635},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-generate-random-numbers-example":3},{"id":4,"title":5,"body":6,"description":1628,"extension":1629,"meta":1630,"navigation":78,"path":1631,"seo":1632,"stem":1633,"__hash__":1634},"content\u002Fexamples\u002Fpython-generate-random-numbers-example.md","Python Generate Random Numbers Example",{"type":7,"value":8,"toc":1591},"minimark",[9,13,22,25,48,51,131,138,143,146,176,184,188,197,243,248,276,279,286,290,297,368,372,386,397,400,492,495,509,512,580,584,591,597,606,646,649,658,664,671,719,722,738,751,757,760,808,810,819,823,826,829,866,869,878,881,890,892,901,904,908,911,917,923,960,963,1009,1016,1023,1033,1079,1082,1126,1134,1137,1158,1162,1165,1172,1181,1184,1201,1205,1212,1215,1264,1273,1277,1280,1294,1301,1305,1309,1318,1354,1362,1372,1408,1416,1432,1435,1487,1491,1494,1556,1560,1587],[10,11,5],"h1",{"id":12},"python-generate-random-numbers-example",[14,15,16,17,21],"p",{},"This beginner-friendly example shows how to generate random numbers in Python with the ",[18,19,20],"code",{},"random"," module.",[14,23,24],{},"You will learn:",[26,27,28,35,38,41],"ul",{},[29,30,31,32,34],"li",{},"How to import the ",[18,33,20],{}," module",[29,36,37],{},"How to generate one random integer",[29,39,40],{},"How to generate several random numbers in a loop",[29,42,43,44,47],{},"How ",[18,45,46],{},"randint()"," differs from other random functions",[14,49,50],{},"If you want the shortest working example, start here:",[52,53,58],"pre",{"className":54,"code":55,"language":56,"meta":57,"style":57},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import random\n\nnumber = random.randint(1, 10)\nprint(number)\n","python","",[18,59,60,73,80,117],{"__ignoreMap":57},[61,62,65,69],"span",{"class":63,"line":64},"line",1,[61,66,68],{"class":67},"sVHd0","import",[61,70,72],{"class":71},"su5hD"," random\n",[61,74,76],{"class":63,"line":75},2,[61,77,79],{"emptyLinePlaceholder":78},true,"\n",[61,81,83,86,90,93,97,101,104,108,111,114],{"class":63,"line":82},3,[61,84,85],{"class":71},"number ",[61,87,89],{"class":88},"smGrS","=",[61,91,92],{"class":71}," random",[61,94,96],{"class":95},"sP7_E",".",[61,98,100],{"class":99},"slqww","randint",[61,102,103],{"class":95},"(",[61,105,107],{"class":106},"srdBf","1",[61,109,110],{"class":95},",",[61,112,113],{"class":106}," 10",[61,115,116],{"class":95},")\n",[61,118,120,124,126,129],{"class":63,"line":119},4,[61,121,123],{"class":122},"sptTA","print",[61,125,103],{"class":95},[61,127,128],{"class":99},"number",[61,130,116],{"class":95},[14,132,133,134,137],{},"Use ",[18,135,136],{},"random.randint(start, end)"," to get a random integer between both numbers, including the endpoints.",[139,140,142],"h2",{"id":141},"what-this-example-shows","What this example shows",[14,144,145],{},"This example focuses on the most common beginner use cases:",[26,147,148,154,157,160],{},[29,149,150,151,153],{},"Importing the ",[18,152,20],{}," module before using it",[29,155,156],{},"Generating a single random whole number",[29,158,159],{},"Repeating that process several times with a loop",[29,161,162,163,165,166,165,169,172,173],{},"Understanding when to use ",[18,164,46],{},", ",[18,167,168],{},"random()",[18,170,171],{},"randrange()",", and ",[18,174,175],{},"uniform()",[14,177,178,179,96],{},"If you are new to the module, see the ",[180,181,183],"a",{"href":182},"\u002Fstandard-library\u002Fpython-random-module-overview\u002F","Python random module overview",[139,185,187],{"id":186},"basic-example-one-random-integer","Basic example: one random integer",[14,189,133,190,193,194,96],{},[18,191,192],{},"import random"," first. Then call ",[18,195,196],{},"random.randint(1, 10)",[52,198,199],{"className":54,"code":55,"language":56,"meta":57,"style":57},[18,200,201,207,211,233],{"__ignoreMap":57},[61,202,203,205],{"class":63,"line":64},[61,204,68],{"class":67},[61,206,72],{"class":71},[61,208,209],{"class":63,"line":75},[61,210,79],{"emptyLinePlaceholder":78},[61,212,213,215,217,219,221,223,225,227,229,231],{"class":63,"line":82},[61,214,85],{"class":71},[61,216,89],{"class":88},[61,218,92],{"class":71},[61,220,96],{"class":95},[61,222,100],{"class":99},[61,224,103],{"class":95},[61,226,107],{"class":106},[61,228,110],{"class":95},[61,230,113],{"class":106},[61,232,116],{"class":95},[61,234,235,237,239,241],{"class":63,"line":119},[61,236,123],{"class":122},[61,238,103],{"class":95},[61,240,128],{"class":99},[61,242,116],{"class":95},[244,245,247],"h3",{"id":246},"how-it-works","How it works",[26,249,250,255,260,270],{},[29,251,252,254],{},[18,253,192],{}," makes the module available",[29,256,257,259],{},[18,258,196],{}," returns a whole number",[29,261,262,263,265,266,269],{},"Both ",[18,264,107],{}," and ",[18,267,268],{},"10"," can be returned",[29,271,272,275],{},[18,273,274],{},"print(number)"," shows the result",[14,277,278],{},"This is a good first example because it is short and easy to test.",[14,280,281,282,96],{},"If you want a full explanation of this function, see ",[180,283,285],{"href":284},"\u002Fstandard-library\u002Frandom.randint-function-explained","random.randint() explained",[139,287,289],{"id":288},"generate-multiple-random-numbers","Generate multiple random numbers",[14,291,292,293,296],{},"You can use a ",[18,294,295],{},"for"," loop to generate several values.",[52,298,300],{"className":54,"code":299,"language":56,"meta":57,"style":57},"import random\n\nfor i in range(5):\n    number = random.randint(1, 10)\n    print(number)\n",[18,301,302,308,312,333,356],{"__ignoreMap":57},[61,303,304,306],{"class":63,"line":64},[61,305,68],{"class":67},[61,307,72],{"class":71},[61,309,310],{"class":63,"line":75},[61,311,79],{"emptyLinePlaceholder":78},[61,313,314,316,319,322,325,327,330],{"class":63,"line":82},[61,315,295],{"class":67},[61,317,318],{"class":71}," i ",[61,320,321],{"class":67},"in",[61,323,324],{"class":122}," range",[61,326,103],{"class":95},[61,328,329],{"class":106},"5",[61,331,332],{"class":95},"):\n",[61,334,335,338,340,342,344,346,348,350,352,354],{"class":63,"line":119},[61,336,337],{"class":71},"    number ",[61,339,89],{"class":88},[61,341,92],{"class":71},[61,343,96],{"class":95},[61,345,100],{"class":99},[61,347,103],{"class":95},[61,349,107],{"class":106},[61,351,110],{"class":95},[61,353,113],{"class":106},[61,355,116],{"class":95},[61,357,359,362,364,366],{"class":63,"line":358},5,[61,360,361],{"class":122},"    print",[61,363,103],{"class":95},[61,365,128],{"class":99},[61,367,116],{"class":95},[244,369,371],{"id":370},"what-this-does","What this does",[26,373,374,380,383],{},[29,375,376,379],{},[18,377,378],{},"range(5)"," repeats the loop 5 times",[29,381,382],{},"Each loop generates a new random integer",[29,384,385],{},"The output is usually different each time",[14,387,388,389,265,393,96],{},"If loops are still new to you, read ",[180,390,392],{"href":391},"\u002Flearn\u002Fpython-for-loops-explained\u002F","Python for loops explained",[180,394,396],{"href":395},"\u002Freference\u002Fpython-range-function-explained\u002F","Python range() function explained",[14,398,399],{},"You can also store the values in a list if you want to use them later:",[52,401,403],{"className":54,"code":402,"language":56,"meta":57,"style":57},"import random\n\nnumbers = []\n\nfor i in range(5):\n    numbers.append(random.randint(1, 10))\n\nprint(numbers)\n",[18,404,405,411,415,425,429,445,475,480],{"__ignoreMap":57},[61,406,407,409],{"class":63,"line":64},[61,408,68],{"class":67},[61,410,72],{"class":71},[61,412,413],{"class":63,"line":75},[61,414,79],{"emptyLinePlaceholder":78},[61,416,417,420,422],{"class":63,"line":82},[61,418,419],{"class":71},"numbers ",[61,421,89],{"class":88},[61,423,424],{"class":95}," []\n",[61,426,427],{"class":63,"line":119},[61,428,79],{"emptyLinePlaceholder":78},[61,430,431,433,435,437,439,441,443],{"class":63,"line":358},[61,432,295],{"class":67},[61,434,318],{"class":71},[61,436,321],{"class":67},[61,438,324],{"class":122},[61,440,103],{"class":95},[61,442,329],{"class":106},[61,444,332],{"class":95},[61,446,448,451,453,456,458,460,462,464,466,468,470,472],{"class":63,"line":447},6,[61,449,450],{"class":71},"    numbers",[61,452,96],{"class":95},[61,454,455],{"class":99},"append",[61,457,103],{"class":95},[61,459,20],{"class":99},[61,461,96],{"class":95},[61,463,100],{"class":99},[61,465,103],{"class":95},[61,467,107],{"class":106},[61,469,110],{"class":95},[61,471,113],{"class":106},[61,473,474],{"class":95},"))\n",[61,476,478],{"class":63,"line":477},7,[61,479,79],{"emptyLinePlaceholder":78},[61,481,483,485,487,490],{"class":63,"line":482},8,[61,484,123],{"class":122},[61,486,103],{"class":95},[61,488,489],{"class":99},"numbers",[61,491,116],{"class":95},[14,493,494],{},"This is useful for:",[26,496,497,500,503,506],{},[29,498,499],{},"Simple games",[29,501,502],{},"Test values",[29,504,505],{},"Sample data",[29,507,508],{},"Practice projects",[14,510,511],{},"You can also write the same idea with a list comprehension:",[52,513,515],{"className":54,"code":514,"language":56,"meta":57,"style":57},"import random\n\nnumbers = [random.randint(1, 10) for _ in range(5)]\nprint(numbers)\n",[18,516,517,523,527,570],{"__ignoreMap":57},[61,518,519,521],{"class":63,"line":64},[61,520,68],{"class":67},[61,522,72],{"class":71},[61,524,525],{"class":63,"line":75},[61,526,79],{"emptyLinePlaceholder":78},[61,528,529,531,533,536,538,540,542,544,546,548,550,553,556,559,561,563,565,567],{"class":63,"line":82},[61,530,419],{"class":71},[61,532,89],{"class":88},[61,534,535],{"class":95}," [",[61,537,20],{"class":71},[61,539,96],{"class":95},[61,541,100],{"class":99},[61,543,103],{"class":95},[61,545,107],{"class":106},[61,547,110],{"class":95},[61,549,113],{"class":106},[61,551,552],{"class":95},")",[61,554,555],{"class":67}," for",[61,557,558],{"class":71}," _ ",[61,560,321],{"class":67},[61,562,324],{"class":122},[61,564,103],{"class":95},[61,566,329],{"class":106},[61,568,569],{"class":95},")]\n",[61,571,572,574,576,578],{"class":63,"line":119},[61,573,123],{"class":122},[61,575,103],{"class":95},[61,577,489],{"class":99},[61,579,116],{"class":95},[139,581,583],{"id":582},"other-common-random-number-options","Other common random number options",[14,585,586,588,589,21],{},[18,587,46],{}," is not the only useful function in the ",[18,590,20],{},[244,592,594],{"id":593},"randomrandom",[18,595,596],{},"random.random()",[14,598,599,600,603,604,96],{},"This returns a decimal number from ",[18,601,602],{},"0"," up to ",[18,605,107],{},[52,607,609],{"className":54,"code":608,"language":56,"meta":57,"style":57},"import random\n\nnumber = random.random()\nprint(number)\n",[18,610,611,617,621,636],{"__ignoreMap":57},[61,612,613,615],{"class":63,"line":64},[61,614,68],{"class":67},[61,616,72],{"class":71},[61,618,619],{"class":63,"line":75},[61,620,79],{"emptyLinePlaceholder":78},[61,622,623,625,627,629,631,633],{"class":63,"line":82},[61,624,85],{"class":71},[61,626,89],{"class":88},[61,628,92],{"class":71},[61,630,96],{"class":95},[61,632,20],{"class":99},[61,634,635],{"class":95},"()\n",[61,637,638,640,642,644],{"class":63,"line":119},[61,639,123],{"class":122},[61,641,103],{"class":95},[61,643,128],{"class":99},[61,645,116],{"class":95},[14,647,648],{},"Example output:",[52,650,652],{"className":54,"code":651,"language":56,"meta":57,"style":57},"0.3478215941\n",[18,653,654],{"__ignoreMap":57},[61,655,656],{"class":63,"line":64},[61,657,651],{"class":106},[244,659,661],{"id":660},"randomrandrangestart-stop",[18,662,663],{},"random.randrange(start, stop)",[14,665,666,667,670],{},"This is similar to how ",[18,668,669],{},"range()"," works.",[52,672,674],{"className":54,"code":673,"language":56,"meta":57,"style":57},"import random\n\nnumber = random.randrange(1, 10)\nprint(number)\n",[18,675,676,682,686,709],{"__ignoreMap":57},[61,677,678,680],{"class":63,"line":64},[61,679,68],{"class":67},[61,681,72],{"class":71},[61,683,684],{"class":63,"line":75},[61,685,79],{"emptyLinePlaceholder":78},[61,687,688,690,692,694,696,699,701,703,705,707],{"class":63,"line":82},[61,689,85],{"class":71},[61,691,89],{"class":88},[61,693,92],{"class":71},[61,695,96],{"class":95},[61,697,698],{"class":99},"randrange",[61,700,103],{"class":95},[61,702,107],{"class":106},[61,704,110],{"class":95},[61,706,113],{"class":106},[61,708,116],{"class":95},[61,710,711,713,715,717],{"class":63,"line":119},[61,712,123],{"class":122},[61,714,103],{"class":95},[61,716,128],{"class":99},[61,718,116],{"class":95},[14,720,721],{},"Important:",[26,723,724,728],{},[29,725,726,269],{},[18,727,107],{},[29,729,730,732,733,737],{},[18,731,268],{}," is ",[734,735,736],"strong",{},"not"," included",[14,739,740,741,744,745,747,748,96],{},"So ",[18,742,743],{},"randrange(1, 10)"," can return numbers from ",[18,746,107],{}," to ",[18,749,750],{},"9",[244,752,754],{"id":753},"randomuniforma-b",[18,755,756],{},"random.uniform(a, b)",[14,758,759],{},"This returns a random decimal number in a range.",[52,761,763],{"className":54,"code":762,"language":56,"meta":57,"style":57},"import random\n\nnumber = random.uniform(1, 10)\nprint(number)\n",[18,764,765,771,775,798],{"__ignoreMap":57},[61,766,767,769],{"class":63,"line":64},[61,768,68],{"class":67},[61,770,72],{"class":71},[61,772,773],{"class":63,"line":75},[61,774,79],{"emptyLinePlaceholder":78},[61,776,777,779,781,783,785,788,790,792,794,796],{"class":63,"line":82},[61,778,85],{"class":71},[61,780,89],{"class":88},[61,782,92],{"class":71},[61,784,96],{"class":95},[61,786,787],{"class":99},"uniform",[61,789,103],{"class":95},[61,791,107],{"class":106},[61,793,110],{"class":95},[61,795,113],{"class":106},[61,797,116],{"class":95},[61,799,800,802,804,806],{"class":63,"line":119},[61,801,123],{"class":122},[61,803,103],{"class":95},[61,805,128],{"class":99},[61,807,116],{"class":95},[14,809,648],{},[52,811,813],{"className":54,"code":812,"language":56,"meta":57,"style":57},"6.284193204\n",[18,814,815],{"__ignoreMap":57},[61,816,817],{"class":63,"line":64},[61,818,812],{"class":106},[139,820,822],{"id":821},"expected-output","Expected output",[14,824,825],{},"Random output changes each time you run the program. That is normal.",[14,827,828],{},"For this code:",[52,830,832],{"className":54,"code":831,"language":56,"meta":57,"style":57},"import random\n\nprint(random.randint(1, 10))\n",[18,833,834,840,844],{"__ignoreMap":57},[61,835,836,838],{"class":63,"line":64},[61,837,68],{"class":67},[61,839,72],{"class":71},[61,841,842],{"class":63,"line":75},[61,843,79],{"emptyLinePlaceholder":78},[61,845,846,848,850,852,854,856,858,860,862,864],{"class":63,"line":82},[61,847,123],{"class":122},[61,849,103],{"class":95},[61,851,20],{"class":99},[61,853,96],{"class":95},[61,855,100],{"class":99},[61,857,103],{"class":95},[61,859,107],{"class":106},[61,861,110],{"class":95},[61,863,113],{"class":106},[61,865,474],{"class":95},[14,867,868],{},"You might see:",[52,870,872],{"className":54,"code":871,"language":56,"meta":57,"style":57},"3\n",[18,873,874],{"__ignoreMap":57},[61,875,876],{"class":63,"line":64},[61,877,871],{"class":106},[14,879,880],{},"Or:",[52,882,884],{"className":54,"code":883,"language":56,"meta":57,"style":57},"8\n",[18,885,886],{"__ignoreMap":57},[61,887,888],{"class":63,"line":64},[61,889,883],{"class":106},[14,891,880],{},[52,893,895],{"className":54,"code":894,"language":56,"meta":57,"style":57},"10\n",[18,896,897],{"__ignoreMap":57},[61,898,899],{"class":63,"line":64},[61,900,894],{"class":106},[14,902,903],{},"Each run can produce a different result, and that is expected behavior.",[139,905,907],{"id":906},"common-beginner-mistakes","Common beginner mistakes",[14,909,910],{},"Here are some common problems when generating random numbers in Python.",[244,912,914,915],{"id":913},"forgetting-to-import-random","Forgetting to import ",[18,916,20],{},[14,918,919,920,96],{},"This causes a ",[18,921,922],{},"NameError",[52,924,926],{"className":54,"code":925,"language":56,"meta":57,"style":57},"number = random.randint(1, 10)\nprint(number)\n",[18,927,928,950],{"__ignoreMap":57},[61,929,930,932,934,936,938,940,942,944,946,948],{"class":63,"line":64},[61,931,85],{"class":71},[61,933,89],{"class":88},[61,935,92],{"class":71},[61,937,96],{"class":95},[61,939,100],{"class":99},[61,941,103],{"class":95},[61,943,107],{"class":106},[61,945,110],{"class":95},[61,947,113],{"class":106},[61,949,116],{"class":95},[61,951,952,954,956,958],{"class":63,"line":75},[61,953,123],{"class":122},[61,955,103],{"class":95},[61,957,128],{"class":99},[61,959,116],{"class":95},[14,961,962],{},"Fix it by adding the import:",[52,964,965],{"className":54,"code":55,"language":56,"meta":57,"style":57},[18,966,967,973,977,999],{"__ignoreMap":57},[61,968,969,971],{"class":63,"line":64},[61,970,68],{"class":67},[61,972,72],{"class":71},[61,974,975],{"class":63,"line":75},[61,976,79],{"emptyLinePlaceholder":78},[61,978,979,981,983,985,987,989,991,993,995,997],{"class":63,"line":82},[61,980,85],{"class":71},[61,982,89],{"class":88},[61,984,92],{"class":71},[61,986,96],{"class":95},[61,988,100],{"class":99},[61,990,103],{"class":95},[61,992,107],{"class":106},[61,994,110],{"class":95},[61,996,113],{"class":106},[61,998,116],{"class":95},[61,1000,1001,1003,1005,1007],{"class":63,"line":119},[61,1002,123],{"class":122},[61,1004,103],{"class":95},[61,1006,128],{"class":99},[61,1008,116],{"class":95},[14,1010,1011,1012,96],{},"If you see this error, read ",[180,1013,1015],{"href":1014},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F","NameError: name is not defined",[244,1017,1019,1020,1022],{"id":1018},"using-randint-without-the-module-name","Using ",[18,1021,46],{}," without the module name",[14,1024,1025,1026,1028,1029,1032],{},"If you write ",[18,1027,192],{},", you must call the function with ",[18,1030,1031],{},"random."," in front:",[52,1034,1035],{"className":54,"code":55,"language":56,"meta":57,"style":57},[18,1036,1037,1043,1047,1069],{"__ignoreMap":57},[61,1038,1039,1041],{"class":63,"line":64},[61,1040,68],{"class":67},[61,1042,72],{"class":71},[61,1044,1045],{"class":63,"line":75},[61,1046,79],{"emptyLinePlaceholder":78},[61,1048,1049,1051,1053,1055,1057,1059,1061,1063,1065,1067],{"class":63,"line":82},[61,1050,85],{"class":71},[61,1052,89],{"class":88},[61,1054,92],{"class":71},[61,1056,96],{"class":95},[61,1058,100],{"class":99},[61,1060,103],{"class":95},[61,1062,107],{"class":106},[61,1064,110],{"class":95},[61,1066,113],{"class":106},[61,1068,116],{"class":95},[61,1070,1071,1073,1075,1077],{"class":63,"line":119},[61,1072,123],{"class":122},[61,1074,103],{"class":95},[61,1076,128],{"class":99},[61,1078,116],{"class":95},[14,1080,1081],{},"This will not work:",[52,1083,1085],{"className":54,"code":1084,"language":56,"meta":57,"style":57},"import random\n\nnumber = randint(1, 10)\nprint(number)\n",[18,1086,1087,1093,1097,1116],{"__ignoreMap":57},[61,1088,1089,1091],{"class":63,"line":64},[61,1090,68],{"class":67},[61,1092,72],{"class":71},[61,1094,1095],{"class":63,"line":75},[61,1096,79],{"emptyLinePlaceholder":78},[61,1098,1099,1101,1103,1106,1108,1110,1112,1114],{"class":63,"line":82},[61,1100,85],{"class":71},[61,1102,89],{"class":88},[61,1104,1105],{"class":99}," randint",[61,1107,103],{"class":95},[61,1109,107],{"class":106},[61,1111,110],{"class":95},[61,1113,113],{"class":106},[61,1115,116],{"class":95},[61,1117,1118,1120,1122,1124],{"class":63,"line":119},[61,1119,123],{"class":122},[61,1121,103],{"class":95},[61,1123,128],{"class":99},[61,1125,116],{"class":95},[244,1127,1129,1130,265,1132],{"id":1128},"confusing-randint-and-randrange","Confusing ",[18,1131,46],{},[18,1133,171],{},[14,1135,1136],{},"These two functions are similar, but they do not handle the end value the same way.",[26,1138,1139,1149],{},[29,1140,1141,1143,1144,1146,1147],{},[18,1142,196],{}," returns ",[18,1145,107],{}," through ",[18,1148,268],{},[29,1150,1151,1143,1154,1146,1156],{},[18,1152,1153],{},"random.randrange(1, 10)",[18,1155,107],{},[18,1157,750],{},[244,1159,1161],{"id":1160},"expecting-the-same-result-every-time","Expecting the same result every time",[14,1163,1164],{},"Random functions usually produce different results on each run. That is the point of using them.",[244,1166,1168,1169],{"id":1167},"naming-your-file-randompy","Naming your file ",[18,1170,1171],{},"random.py",[14,1173,1174,1175,1177,1178,96],{},"If your script is named ",[18,1176,1171],{},", Python may try to import your file instead of the standard library module. This can cause strange errors such as an ",[18,1179,1180],{},"AttributeError",[14,1182,1183],{},"Rename the file to something else, such as:",[26,1185,1186,1191,1196],{},[29,1187,1188],{},[18,1189,1190],{},"generate_numbers.py",[29,1192,1193],{},[18,1194,1195],{},"random_example.py",[29,1197,1198],{},[18,1199,1200],{},"test_random_values.py",[244,1202,1204],{"id":1203},"using-float-values-with-functions-that-expect-integers","Using float values with functions that expect integers",[14,1206,1207,265,1209,1211],{},[18,1208,46],{},[18,1210,171],{}," are meant for integer ranges.",[14,1213,1214],{},"This is not correct:",[52,1216,1218],{"className":54,"code":1217,"language":56,"meta":57,"style":57},"import random\n\nnumber = random.randint(1.5, 10.5)\nprint(number)\n",[18,1219,1220,1226,1230,1254],{"__ignoreMap":57},[61,1221,1222,1224],{"class":63,"line":64},[61,1223,68],{"class":67},[61,1225,72],{"class":71},[61,1227,1228],{"class":63,"line":75},[61,1229,79],{"emptyLinePlaceholder":78},[61,1231,1232,1234,1236,1238,1240,1242,1244,1247,1249,1252],{"class":63,"line":82},[61,1233,85],{"class":71},[61,1235,89],{"class":88},[61,1237,92],{"class":71},[61,1239,96],{"class":95},[61,1241,100],{"class":99},[61,1243,103],{"class":95},[61,1245,1246],{"class":106},"1.5",[61,1248,110],{"class":95},[61,1250,1251],{"class":106}," 10.5",[61,1253,116],{"class":95},[61,1255,1256,1258,1260,1262],{"class":63,"line":119},[61,1257,123],{"class":122},[61,1259,103],{"class":95},[61,1261,128],{"class":99},[61,1263,116],{"class":95},[14,1265,1266,1267,1269,1270,1272],{},"Use integers with ",[18,1268,46],{},", or use ",[18,1271,175],{}," for decimal values.",[139,1274,1276],{"id":1275},"when-to-use-this","When to use this",[14,1278,1279],{},"Generating random numbers is useful in many beginner projects, including:",[26,1281,1282,1285,1288,1291],{},[29,1283,1284],{},"Picking a random number for a game",[29,1286,1287],{},"Creating test values",[29,1289,1290],{},"Choosing random positions or delays",[29,1292,1293],{},"Building practice projects",[14,1295,1296,1297,96],{},"A common next step is a guessing game. Try this related example: ",[180,1298,1300],{"href":1299},"\u002Fexamples\u002Fpython-number-guessing-game-example\u002F","Python number guessing game example",[139,1302,1304],{"id":1303},"faq","FAQ",[244,1306,1308],{"id":1307},"how-do-i-generate-a-random-number-between-1-and-10-in-python","How do I generate a random number between 1 and 10 in Python?",[14,1310,133,1311,1313,1314,265,1316,96],{},[18,1312,196],{},". This includes both ",[18,1315,107],{},[18,1317,268],{},[52,1319,1320],{"className":54,"code":831,"language":56,"meta":57,"style":57},[18,1321,1322,1328,1332],{"__ignoreMap":57},[61,1323,1324,1326],{"class":63,"line":64},[61,1325,68],{"class":67},[61,1327,72],{"class":71},[61,1329,1330],{"class":63,"line":75},[61,1331,79],{"emptyLinePlaceholder":78},[61,1333,1334,1336,1338,1340,1342,1344,1346,1348,1350,1352],{"class":63,"line":82},[61,1335,123],{"class":122},[61,1337,103],{"class":95},[61,1339,20],{"class":99},[61,1341,96],{"class":95},[61,1343,100],{"class":99},[61,1345,103],{"class":95},[61,1347,107],{"class":106},[61,1349,110],{"class":95},[61,1351,113],{"class":106},[61,1353,474],{"class":95},[244,1355,1357,1358,1361],{"id":1356},"why-do-i-get-nameerror-name-random-is-not-defined","Why do I get ",[18,1359,1360],{},"NameError: name 'random' is not defined","?",[14,1363,1364,1365,1367,1368,1371],{},"You need to add ",[18,1366,192],{}," before using ",[18,1369,1370],{},"random.randint()"," or other random functions.",[52,1373,1374],{"className":54,"code":831,"language":56,"meta":57,"style":57},[18,1375,1376,1382,1386],{"__ignoreMap":57},[61,1377,1378,1380],{"class":63,"line":64},[61,1379,68],{"class":67},[61,1381,72],{"class":71},[61,1383,1384],{"class":63,"line":75},[61,1385,79],{"emptyLinePlaceholder":78},[61,1387,1388,1390,1392,1394,1396,1398,1400,1402,1404,1406],{"class":63,"line":82},[61,1389,123],{"class":122},[61,1391,103],{"class":95},[61,1393,20],{"class":99},[61,1395,96],{"class":95},[61,1397,100],{"class":99},[61,1399,103],{"class":95},[61,1401,107],{"class":106},[61,1403,110],{"class":95},[61,1405,113],{"class":106},[61,1407,474],{"class":95},[244,1409,1411,1412,265,1414,1361],{"id":1410},"what-is-the-difference-between-randint-and-random","What is the difference between ",[18,1413,46],{},[18,1415,168],{},[26,1417,1418,1423],{},[29,1419,1420,1422],{},[18,1421,46],{}," gives a whole number in a range",[29,1424,1425,1427,1428,265,1430],{},[18,1426,168],{}," gives a decimal number between ",[18,1429,602],{},[18,1431,107],{},[14,1433,1434],{},"Example:",[52,1436,1438],{"className":54,"code":1437,"language":56,"meta":57,"style":57},"import random\n\nprint(random.randint(1, 10))\nprint(random.random())\n",[18,1439,1440,1446,1450,1472],{"__ignoreMap":57},[61,1441,1442,1444],{"class":63,"line":64},[61,1443,68],{"class":67},[61,1445,72],{"class":71},[61,1447,1448],{"class":63,"line":75},[61,1449,79],{"emptyLinePlaceholder":78},[61,1451,1452,1454,1456,1458,1460,1462,1464,1466,1468,1470],{"class":63,"line":82},[61,1453,123],{"class":122},[61,1455,103],{"class":95},[61,1457,20],{"class":99},[61,1459,96],{"class":95},[61,1461,100],{"class":99},[61,1463,103],{"class":95},[61,1465,107],{"class":106},[61,1467,110],{"class":95},[61,1469,113],{"class":106},[61,1471,474],{"class":95},[61,1473,1474,1476,1478,1480,1482,1484],{"class":63,"line":119},[61,1475,123],{"class":122},[61,1477,103],{"class":95},[61,1479,20],{"class":99},[61,1481,96],{"class":95},[61,1483,20],{"class":99},[61,1485,1486],{"class":95},"())\n",[244,1488,1490],{"id":1489},"can-i-generate-several-random-numbers-at-once","Can I generate several random numbers at once?",[14,1492,1493],{},"Yes. Use a loop or a list comprehension.",[52,1495,1496],{"className":54,"code":514,"language":56,"meta":57,"style":57},[18,1497,1498,1504,1508,1546],{"__ignoreMap":57},[61,1499,1500,1502],{"class":63,"line":64},[61,1501,68],{"class":67},[61,1503,72],{"class":71},[61,1505,1506],{"class":63,"line":75},[61,1507,79],{"emptyLinePlaceholder":78},[61,1509,1510,1512,1514,1516,1518,1520,1522,1524,1526,1528,1530,1532,1534,1536,1538,1540,1542,1544],{"class":63,"line":82},[61,1511,419],{"class":71},[61,1513,89],{"class":88},[61,1515,535],{"class":95},[61,1517,20],{"class":71},[61,1519,96],{"class":95},[61,1521,100],{"class":99},[61,1523,103],{"class":95},[61,1525,107],{"class":106},[61,1527,110],{"class":95},[61,1529,113],{"class":106},[61,1531,552],{"class":95},[61,1533,555],{"class":67},[61,1535,558],{"class":71},[61,1537,321],{"class":67},[61,1539,324],{"class":122},[61,1541,103],{"class":95},[61,1543,329],{"class":106},[61,1545,569],{"class":95},[61,1547,1548,1550,1552,1554],{"class":63,"line":119},[61,1549,123],{"class":122},[61,1551,103],{"class":95},[61,1553,489],{"class":99},[61,1555,116],{"class":95},[139,1557,1559],{"id":1558},"see-also","See also",[26,1561,1562,1566,1570,1574,1578,1582],{},[29,1563,1564],{},[180,1565,183],{"href":182},[29,1567,1568],{},[180,1569,285],{"href":284},[29,1571,1572],{},[180,1573,396],{"href":395},[29,1575,1576],{},[180,1577,392],{"href":391},[29,1579,1580],{},[180,1581,1300],{"href":1299},[29,1583,1584],{},[180,1585,1586],{"href":1014},"NameError: name is not defined fix",[1588,1589,1590],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}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);}",{"title":57,"searchDepth":75,"depth":75,"links":1592},[1593,1594,1597,1600,1605,1606,1618,1619,1627],{"id":141,"depth":75,"text":142},{"id":186,"depth":75,"text":187,"children":1595},[1596],{"id":246,"depth":82,"text":247},{"id":288,"depth":75,"text":289,"children":1598},[1599],{"id":370,"depth":82,"text":371},{"id":582,"depth":75,"text":583,"children":1601},[1602,1603,1604],{"id":593,"depth":82,"text":596},{"id":660,"depth":82,"text":663},{"id":753,"depth":82,"text":756},{"id":821,"depth":75,"text":822},{"id":906,"depth":75,"text":907,"children":1607},[1608,1610,1612,1614,1615,1617],{"id":913,"depth":82,"text":1609},"Forgetting to import random",{"id":1018,"depth":82,"text":1611},"Using randint() without the module name",{"id":1128,"depth":82,"text":1613},"Confusing randint() and randrange()",{"id":1160,"depth":82,"text":1161},{"id":1167,"depth":82,"text":1616},"Naming your file random.py",{"id":1203,"depth":82,"text":1204},{"id":1275,"depth":75,"text":1276},{"id":1303,"depth":75,"text":1304,"children":1620},[1621,1622,1624,1626],{"id":1307,"depth":82,"text":1308},{"id":1356,"depth":82,"text":1623},"Why do I get NameError: name 'random' is not defined?",{"id":1410,"depth":82,"text":1625},"What is the difference between randint() and random()?",{"id":1489,"depth":82,"text":1490},{"id":1558,"depth":75,"text":1559},"Master python generate random numbers example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-generate-random-numbers-example",{"title":5,"description":1628},"examples\u002Fpython-generate-random-numbers-example","wBuYGdtmBnLM3b0St2JTFh8YXIYdEUfqxGKYMJwSc3s",1777585475145]