{"id":115294,"date":"2023-11-24T08:05:37","date_gmt":"2023-11-24T11:05:37","guid":{"rendered":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/"},"modified":"2023-11-24T08:05:37","modified_gmt":"2023-11-24T11:05:37","slug":"biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python","status":"publish","type":"post","link":"https:\/\/homolog.fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/","title":{"rendered":"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python"},"content":{"rendered":"<p><!DOCTYPE html><br \/>\n<html><br \/>\n<head><br \/>\n<\/head><br \/>\n<body><\/p>\n<h1>O que \u00e9 a biblioteca random no Python?<\/h1>\n<h2>Introdu\u00e7\u00e3o<\/h2>\n<p>A biblioteca random \u00e9 uma biblioteca padr\u00e3o do Python que oferece uma s\u00e9rie de fun\u00e7\u00f5es relacionadas \u00e0 gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios. Ela permite que os desenvolvedores criem programas com elementos de aleatoriedade, trazendo um aspecto de imprevisibilidade e variedade \u00e0s aplica\u00e7\u00f5es.<\/p>\n<h2>Utilizando a biblioteca random<\/h2>\n<p>Para utilizar a biblioteca random no Python, \u00e9 necess\u00e1rio import\u00e1-la no in\u00edcio do c\u00f3digo. Isso pode ser feito utilizando o comando <code>import random<\/code>. Ap\u00f3s importar a biblioteca, \u00e9 poss\u00edvel utilizar suas fun\u00e7\u00f5es em qualquer parte do programa.<\/p>\n<h3>Principais fun\u00e7\u00f5es da biblioteca random<\/h3>\n<p>A biblioteca random no Python possui uma ampla gama de fun\u00e7\u00f5es que podem ser utilizadas para gerar n\u00fameros aleat\u00f3rios e realizar opera\u00e7\u00f5es relacionadas. Vamos conhecer algumas das principais fun\u00e7\u00f5es:<\/p>\n<ul>\n<li><strong>random()<\/strong>: retorna um n\u00famero real aleat\u00f3rio entre 0 e 1.<\/li>\n<li><strong>randint(a, b)<\/strong>: retorna um n\u00famero inteiro aleat\u00f3rio entre a e b (inclusive).<\/li>\n<li><strong>choice(seq)<\/strong>: retorna um elemento aleat\u00f3rio de uma sequ\u00eancia.<\/li>\n<li><strong>shuffle(seq)<\/strong>: embaralha os elementos de uma sequ\u00eancia.<\/li>\n<li><strong>sample(seq, k)<\/strong>: retorna uma lista com k elementos aleat\u00f3rios de uma sequ\u00eancia, sem repeti\u00e7\u00f5es.<\/li>\n<\/ul>\n<h3>Exemplos pr\u00e1ticos de uso da biblioteca random<\/h3>\n<p>Vamos agora ver alguns exemplos pr\u00e1ticos de como utilizar a biblioteca random no Python:<\/p>\n<p><strong>Exemplo 1: Gerando um n\u00famero aleat\u00f3rio entre 1 e 10<\/strong><\/p>\n<pre><code>import random\n\nnumero = random.randint(1, 10)\nprint(numero)\n<\/code><\/pre>\n<p><strong>Exemplo 2: Embaralhando uma lista de cartas<\/strong><\/p>\n<pre><code>import random\n\ncartas = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']\nrandom.shuffle(cartas)\nprint(cartas)\n<\/code><\/pre>\n<p><strong>Exemplo 3: Selecionando amostras aleat\u00f3rias de uma popula\u00e7\u00e3o<\/strong><\/p>\n<pre><code>import random\n\npopulacao = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\namostra = random.sample(populacao, 3)\nprint(amostra)\n<\/code><\/pre>\n<h2>Conclus\u00e3o<\/h2>\n<p>A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5es, \u00e9 poss\u00edvel criar programas com elementos de aleatoriedade, trazendo variedade e imprevisibilidade \u00e0s aplica\u00e7\u00f5es.<\/p>\n<p><\/body><br \/>\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5es, \u00e9 poss\u00edvel criar programas com elementos de aleatoriedade, trazendo variedade e imprevisibilidade \u00e0s aplica\u00e7\u00f5es. Neste artigo, aprendemos o que \u00e9 a biblioteca random no Python, como import\u00e1-la e utiliz\u00e1-la em nossos programas. Exploramos algumas das principais fun\u00e7\u00f5es dispon\u00edveis, como random(), randint(a, b), choice(seq), shuffle(seq) e sample(seq, k), e vimos exemplos pr\u00e1ticos de uso. Agora que voc\u00ea conhece um pouco mais sobre a biblioteca random no Python, aproveite esse conhecimento e explore suas possibilidades em seus projetos. Aprenda a utilizar a Biblioteca Random Python e adicione elementos de aleatoriedade aos seus programas, tornando-os mais din\u00e2micos e interessantes.<\/p>\n","protected":false},"author":9,"featured_media":27569,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":[186],"meta":{"inline_featured_image":false,"footnotes":""},"categories":[229],"tags":[],"trilha":[],"class_list":["post-115294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-skills","format-artigos"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python - Fluency.io Brasil<\/title>\n<meta name=\"description\" content=\"A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5e...\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python - Fluency.io Brasil\" \/>\n<meta property=\"og:description\" content=\"A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5e...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Fluency.io Brasil\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-24T11:05:37+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"kaue\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. tempo de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/\"},\"author\":{\"name\":\"kaue\",\"@id\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/#\\\/schema\\\/person\\\/7b3b2b50ba17b7f2ad0cce0a40bfa00a\"},\"headline\":\"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python\",\"datePublished\":\"2023-11-24T11:05:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/\"},\"wordCount\":305,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"articleSection\":[\"Skills\"],\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/\",\"url\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/\",\"name\":\"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python - Fluency.io Brasil\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"datePublished\":\"2023-11-24T11:05:37+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/#\\\/schema\\\/person\\\/7b3b2b50ba17b7f2ad0cce0a40bfa00a\"},\"description\":\"A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5e...\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#primaryimage\",\"url\":\"\",\"contentUrl\":\"\",\"width\":1027,\"height\":420},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fluency.io\\\/br\\\/blog\\\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"In\u00edcio\",\"item\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/#website\",\"url\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/\",\"name\":\"Fluency.io Brasil\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/#\\\/schema\\\/person\\\/7b3b2b50ba17b7f2ad0cce0a40bfa00a\",\"name\":\"kaue\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bdd3958fa53019cfd8f789c0a49a730e7ba40a1d20cb42c9ced7646285842479?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bdd3958fa53019cfd8f789c0a49a730e7ba40a1d20cb42c9ced7646285842479?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/bdd3958fa53019cfd8f789c0a49a730e7ba40a1d20cb42c9ced7646285842479?s=96&d=mm&r=g\",\"caption\":\"kaue\"},\"url\":\"https:\\\/\\\/homolog.fluency.io\\\/br\\\/author\\\/kaue\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python - Fluency.io Brasil","description":"A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5e...","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"pt_BR","og_type":"article","og_title":"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python - Fluency.io Brasil","og_description":"A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5e...","og_url":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/","og_site_name":"Fluency.io Brasil","article_published_time":"2023-11-24T11:05:37+00:00","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"kaue","Est. tempo de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#article","isPartOf":{"@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/"},"author":{"name":"kaue","@id":"https:\/\/homolog.fluency.io\/br\/#\/schema\/person\/7b3b2b50ba17b7f2ad0cce0a40bfa00a"},"headline":"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python","datePublished":"2023-11-24T11:05:37+00:00","mainEntityOfPage":{"@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/"},"wordCount":305,"commentCount":0,"image":{"@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#primaryimage"},"thumbnailUrl":"","articleSection":["Skills"],"inLanguage":"pt-BR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/","url":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/","name":"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python - Fluency.io Brasil","isPartOf":{"@id":"https:\/\/homolog.fluency.io\/br\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#primaryimage"},"image":{"@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#primaryimage"},"thumbnailUrl":"","datePublished":"2023-11-24T11:05:37+00:00","author":{"@id":"https:\/\/homolog.fluency.io\/br\/#\/schema\/person\/7b3b2b50ba17b7f2ad0cce0a40bfa00a"},"description":"A biblioteca random no Python \u00e9 uma ferramenta poderosa para gera\u00e7\u00e3o de n\u00fameros aleat\u00f3rios e realiza\u00e7\u00e3o de opera\u00e7\u00f5es relacionadas. Com suas diversas fun\u00e7\u00f5e...","breadcrumb":{"@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#primaryimage","url":"","contentUrl":"","width":1027,"height":420},{"@type":"BreadcrumbList","@id":"https:\/\/fluency.io\/br\/blog\/biblioteca-random-python-aprenda-a-utilizar-a-biblioteca-random-no-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"In\u00edcio","item":"https:\/\/homolog.fluency.io\/br\/"},{"@type":"ListItem","position":2,"name":"Biblioteca Random Python: Aprenda a Utilizar a Biblioteca Random no Python"}]},{"@type":"WebSite","@id":"https:\/\/homolog.fluency.io\/br\/#website","url":"https:\/\/homolog.fluency.io\/br\/","name":"Fluency.io Brasil","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/homolog.fluency.io\/br\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Person","@id":"https:\/\/homolog.fluency.io\/br\/#\/schema\/person\/7b3b2b50ba17b7f2ad0cce0a40bfa00a","name":"kaue","image":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/secure.gravatar.com\/avatar\/bdd3958fa53019cfd8f789c0a49a730e7ba40a1d20cb42c9ced7646285842479?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/bdd3958fa53019cfd8f789c0a49a730e7ba40a1d20cb42c9ced7646285842479?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bdd3958fa53019cfd8f789c0a49a730e7ba40a1d20cb42c9ced7646285842479?s=96&d=mm&r=g","caption":"kaue"},"url":"https:\/\/homolog.fluency.io\/br\/author\/kaue\/"}]}},"_links":{"self":[{"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/posts\/115294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/comments?post=115294"}],"version-history":[{"count":0,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/posts\/115294\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/media?parent=115294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/categories?post=115294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/tags?post=115294"},{"taxonomy":"format","embeddable":true,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/format?post=115294"},{"taxonomy":"trilha","embeddable":true,"href":"https:\/\/homolog.fluency.io\/br\/wp-json\/wp\/v2\/trilha?post=115294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}