{"id":3203,"date":"2022-12-05T12:25:42","date_gmt":"2022-12-05T12:25:42","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=3203"},"modified":"2024-12-06T12:15:36","modified_gmt":"2024-12-06T12:15:36","slug":"how-to-generate-a-random-number-in-solidity","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/how-to-generate-a-random-number-in-solidity\/","title":{"rendered":"How to Generate a Random Number in Solidity? \u2014 A Comprehensive Guide"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/how-to-generate-a-random-number-in-solidity\/#Follow_these_steps_to_generate_a_random_number_in_Solidity\" title=\"Follow these steps to generate a random number in Solidity\">Follow these steps to generate a random number in Solidity<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><em>This answer will help you understand how to generate a random number within your <a href=\"https:\/\/metaschool.so\/solidity?utm_campaign=smf&amp;utm_source=blog&amp;utm_medium=organic\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity<\/a> smart contract.<\/em><\/p>\n\n\n\n<p>Number generation in Solidity is the process of creating random numbers within a <a href=\"https:\/\/metaschool.so\/courses\/writing-your-first-hello-world-contract-in-solidity?ref=Articles&amp;utm_source=Blog_Organic\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity smart contract<\/a>. This can be used for various purposes, such as generating unique IDs for objects, creating random outcomes in games, or selecting random participants for a lottery. <\/p>\n\n\n\n<p>Number generation is an important feature of Solidity as it allows for greater flexibility and functionality in smart contracts. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"follow-these-steps-to-generate-a-random-number-in-solidity\"><span class=\"ez-toc-section\" id=\"Follow_these_steps_to_generate_a_random_number_in_Solidity\"><\/span>Follow these steps to generate a random number in Solidity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-import-the-keccak256-library-by-adding-the-following-line-to-the-beginning-of-your-contract\">1. Import the <code>keccak256<\/code> library by adding the following line to the beginning of your contract<\/h3>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>pragma solidity ^0.4.24;\nimport \"https:\/\/github.com\/OpenZeppelin\/openzeppelin-solidity\/contracts\/math\/SafeMath.sol\";\nimport \"https:\/\/github.com\/OpenZeppelin\/openzeppelin-solidity\/contracts\/cryptography\/Keccak256.sol\";\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-create-a-function-named-generaterandomnumber-that-takes-in-no-arguments-and-returns-a-uint-value\">2. Create a function named <code>generateRandomNumber<\/code> that takes in no arguments and returns a <code>uint<\/code> value<\/h3>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>function generateRandomNumber() public view returns (uint) {\n    \/\/ code to generate random number goes here\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-inside-the-generaterandomnumber-function-create-a-variable-named-timestamp-that-stores-the-current-block-s-timestamp\">3. Inside the <code>generateRandomNumber<\/code> function, create a variable named <code>timestamp<\/code> that stores the current block&#8217;s timestamp<\/h3>\n\n\n\n<p>This can be obtained using the <code>block.timestamp<\/code> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>function generateRandomNumber() public view returns (uint) {\n    uint timestamp = block.timestamp;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-use-the-keccak256-library-to-hash-the-timestamp-variable-and-store-the-resulting-hash-in-a-bytes32-variable-named-hash\">4. Use the <code>keccak256<\/code> library to hash the <code>timestamp<\/code> variable and store the resulting hash in a <code>bytes32<\/code> variable named <code>hash<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>function generateRandomNumber() public view returns (uint) {\n    uint timestamp = block.timestamp;\n    bytes32 hash = keccak256(abi.encodePacked(timestamp));\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-use-the-bytes32-type-s-uint-function-to-convert-the-hash-value-to-a-uint-value-and-return-it\">5. Use the <code>bytes32<\/code> type&#8217;s <code>uint()<\/code> function to convert the <code>hash<\/code> value to a <code>uint<\/code> value and return it<\/h3>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>function generateRandomNumber() public view returns (uint) {\n    uint timestamp = block.timestamp;\n    bytes32 hash = keccak256(abi.encodePacked(timestamp));\n    return hash.uint();\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-to-use-this-function-in-your-contract-simply-call-it-and-store-the-returned-value-in-a-uint-variable\">6. To use this function in your contract, simply call it and store the returned value in a <code>uint<\/code> variable<\/h3>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>function someFunction() public {\n    uint randomNumber = generateRandomNumber();\n}\n<\/code><\/pre>\n\n\n\n<p>Note: The randomness of this method is dependent on the block timestamp, which is not necessarily truly random. It is recommended to use a more secure method of generating random numbers, such as using a random number oracle service.<\/p>\n\n\n\n<p>Hopefully this answer helps you understand how to generate a random number in <a href=\"https:\/\/docs.soliditylang.org\/en\/v0.8.28\/\" target=\"_blank\" rel=\"noopener\">Solidity<\/a>. If you have any suggestions, feedback or got something nice to say \u2013 leave a comment below and say hi \ud83d\udc4b\ud83c\udffc<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":11066,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[31,46],"class_list":["post-3203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-ethereum-blockchain","tag-solidity"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=3203"}],"version-history":[{"count":7,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3203\/revisions"}],"predecessor-version":[{"id":11067,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3203\/revisions\/11067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/11066"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=3203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=3203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=3203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}