📜 [專欄新文章] Gas Efficient Card Drawing in Solidity
✍️ Ping Chen
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Assign random numbers as the index of newly minted NFTs
Scenario
The fun of generative art NFT projects depends on randomness. The industry standard is “blind box”, where both the images’ serial number and the NFTs’ index are predetermined but will be shifted randomly when the selling period ends. (They call it “reveal”) This approach effectively solves the randomness issue. However, it also requires buyers to wait until the campaign terminates. What if buyers want to know the exact card right away? We’ll need a reliable onchain card drawing solution.
The creator of Astrogator🐊 isn’t a fan of blind boxes; instead, it thinks unpacking cards right after purchase is more interesting.
Spec
When initializing this NFT contract, the creator will determine the total supply of it. And there will be an iterable function that is randomly picking a number from the remaining pool. The number must be in range and must not collide with any existing ones.
Our top priority is accessibility/gas efficiency. Given that gas cost on Ethereum is damn high nowadays, we need an elegant algorithm to control gas expanse at an acceptable range.
Achieving robust randomness isn’t the primary goal here. We assume there’s no strong financial incentive to cheat, so the RNG isn’t specified. Implementers can bring their own source of randomness that they think is good enough.
Implementation
Overview
The implementation is pretty short and straightforward. Imagine there’s an array that contains all remaining(unsold) cards. When drawIndex() is called, it generates a (uniform) random seed to draw a card from the array, shortens the array, and returns the selected card.
Algorithm
Drawing X cards from a deck with the same X amount of cards is equal to shuffling the deck and dealing them sequentially. It’s not a surprise that our algorithm is similar to random shuffling, and the only difference is turning that classic algo into an interactive version.
A typical random shuffle looks like this: for an array with N elements, you randomly pick a number i in (0,N), swap array[0] and array[i], then choose another number i in (1,N), swap array[1] and array[i], and so on. Eventually, you’ll get a mathematically random array in O(N) time.
So, the concept of our random card dealing is the same. When a user mints a new card, the smart contract picks a number in the array as NFT index, then grabs a number from the tail to fill the vacancy, in order to keep the array continuous.
Tweak
Furthermore, as long as the space of the NFT index is known, we don’t need to declare/initialize an array(which is super gas-intensive). Instead, assume there’s such an array that the n-th element is n, we don’t actually initialize it (so it is an array only contains “0”) until the rule is broken.
For the convenience of explanation, let’s call that mapping cache. If cache[i] is empty, it should be interpreted as i instead of 0. On the other hand, when a number is chosen and used, we’ll need to fill it up with another unused number. An intuitive method is to pick a number from the end of the array, since the length of the array is going to decrease by 1.
By doing so, the gas cost in the worst-case scenario is bound to be constant.
Performance and limitation
Comparing with the normal ascending index NFT minting, our random NFT implementation requires two extra SSTORE and one extra SLOAD, which cost 12600 ~ 27600 (5000+20000+2600) excess gas per token minted.
Theoretically, any instantly generated onchain random number is vulnerable. We can restrict contract interaction to mitigate risk. The mitigation is far from perfect, but it is the tradeoff that we have to accept.
ping.eth
Gas Efficient Card Drawing in Solidity was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有84部Youtube影片,追蹤數超過3萬的網紅Ah LAI Channel,也在其Youtube影片中提到,...
「number教學」的推薦目錄:
- 關於number教學 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於number教學 在 國立臺灣大學 National Taiwan University Facebook 的最讚貼文
- 關於number教學 在 賭Sir(杜氏數學) Facebook 的最佳貼文
- 關於number教學 在 Ah LAI Channel Youtube 的最佳解答
- 關於number教學 在 YOYOTV Youtube 的最佳解答
- 關於number教學 在 Aotter Girls: Girl's Tech Talk Youtube 的最佳貼文
- 關於number教學 在 Excel教學- Cells儲存格stored as text/number - Advanced Level... 的評價
number教學 在 國立臺灣大學 National Taiwan University Facebook 的最讚貼文
【教務處開學防疫相關措施】
Relevant prevention measures for the upcoming semester of the Office of Academic Affairs
.
各位好:
因應嚴重特殊傳染性肺炎疫情發展,除校方發布各項防疫措施外,有關教學及其相關申辦業務,請各單位依據以下措施配合辦理:
一、110-1學期開學後前三週全面採遠距教學,授課教師應將上課網址及課程相關訊息公告於課程大綱 (有關NTU COOL課程網頁如何設為公開,詳見 https://www.dlc.ntu.edu.tw/2021/09/09/visibility/ )。實驗、實作或人數較少的研究所課程若需採實體授課,須依以下規範辦理:
(一) 落實課堂點名並禁止在教室內飲食。
(二) 上課教室應保持通風良好及定時清消。
(三) 全程配戴口罩,並維持安全社交距離 (1.5米或2.25平方米)。
(四)於110年9月24日前提出申請送教務處核定:申請表詳見課務組公告 https://www.space.ntu.edu.tw/navigate/s/CB679CFFA6134C83B4964E40A30A460AQQY
二、開學後第四週起上課方式,修課人數80人(含)以上課程採遠距教學,80人以下以實體課程為原則,特殊情形請於110年10月12日前提出申請送教務處核定:申請表詳見課務組公告 https://www.space.ntu.edu.tw/navigate/s/CB679CFFA6134C83B4964E40A30A460AQQY
(一) 採實體授課者,仍須依前述防疫規範辦理。
(二)本校鼓勵教師以數位和實體並行的混成方式進行教學準備,詳細資訊請見數習中心網站: https://www.dlc.ntu.edu.tw/2021/09/08/blended-teaching/
三、本學期加退選、停修彈性做法如下:
(一) 解除擋修、超修及減修申請書: 老師及系主任可親簽或用郵件代替簽名,請同學將申請書及相關同意證明以email寄至所屬教務單位承辦人。
(二) 開學後第一、二週加退選:
1.第1、3類方式加選:照舊(上網登記分發)。
2.第2類方式加選(授權碼): 建議老師可用信件傳遞授權碼予學生加選,並記錄學號留存,以利日後查核。
(三) 開學後第三週人工加選及停修:老師及系主任可親簽或用郵件代替簽名,請同學將申請書及相關同意證明,合併成1個pdf檔上傳至教務處。上傳網址將再另行公告。
(四) 因本學期前3週皆採遠距授課,同學在加退選期間若有加簽或聽課需求,請主動聯繫授課老師。
.
教務處 敬上
.
Dear NTU community members,
Given the current pandemic situation, the University is asking all academic and administrative units to comply with the following measures regarding teaching and learning for the upcoming semester despite the epidemic prevention measures released by the school:
A. All courses shall adopt distance learning in the first three weeks since 110-1 semester. Teachers shall announce course URL and relevant information on course description.(If you want to set the visibility of your course page on NTU COOL to be public, please see this document: https://www.dlc.ntu.edu.tw/2021/09/09/visibility/ ) If experiments, hands-on course,graduate courses with fewer people,wish to give physical course, please follow the measures listed below:
1. Teachers shall do the roll call, while eating and drinking in the classroom is forbidden.
2. Classrooms shall be cleansed and disinfected regularly, while kept ventilated.
3. Face masks must be worn throughout, while social distancing shall be maintained (e.g., 1.5 meters or 2.25 square meters).
4. Please submit an application form ( https://www.space.ntu.edu.tw/navigate/s/CB679CFFA6134C83B4964E40A30A460AQQY ) to the Office of Academic Affairs before September 24th.
B. In principle, 80 or above people selecting a course shall adopt distance learning from the fourth week of 110-1 semester. Courses under 80 people will be in-person classes. In special cases, please fill out this application form ( https://www.space.ntu.edu.tw/navigate/s/CB679CFFA6134C83B4964E40A30A460AQQY ) before October 12th.
1. In-person class shall comply with the regulations listed above.
2. Teachers are encouraged to adopt a blended teaching approach -a combination of online learning and in-person class - to prepare the courses. For more information, please see the website of Digital Learning Center: https://www.dlc.ntu.edu.tw/2021/09/08/blended-teaching/
C. The flexible measures of this semester for adding, dropping, and withdrawing classes are as follows.
1. The “Unblock Course Request Form”, “Course Overload Request Form”, and “Course Underload Request Form”:
The lecturer and the director may sign these request forms in person or reply by email to these request forms to indicate their approval of the request. Please email the request form and the proof for approval to the contact person in the Office of Academic Affairs.
2. Adding and dropping classes in the first and second week of this semester:
(1) . Adding classes in the first and the third category:
The process is as usual. Students may register for classes online.
(2). The second category (adding classes with a permission number):
It is recommended that the lecturer email a permission number to students and record the student number for future verification.
3. Adding or withdrawing classes in the third week of this semester:
The lecturer and the director may sign the application form in person or reply by email to the application form to indicate their approval of the request. Please combine the application form and the proof for approval into a single PDF and upload it to the Office of Academic Affairs. The website address for uploading the file will be announced separately.
4. Because online teaching is conducted during the first three weeks of this semester, if students want to add or attend the classes during the period, they need to contact the lecturer.
.
Office of Academic Affairs
number教學 在 賭Sir(杜氏數學) Facebook 的最佳貼文
首先,盤古初開有天有地有盤古,但係無質數(Prime Number)存在,所有數學概念都係人製造出嚟😌 換言之,製造嘅人必然有佢嘅目的、有佢嘅考量,其實包同唔包都得,正如你早餐食飯定食麵都唔會死,但你梗係會食個好味啲嘅😋
後來,大家都決定,質數係唔包1嘅,最細嘅質數係2,然後一路數落去:2、3、5、7、11、13⋯⋯全部都只能被1以及自己整除。
有咩好處呢?就係天下間嘅所有正整數,都能夠寫成呢堆數字相乘,專稱Prime Factorization。
尤其是將所有質數由細至大排,再填佢哋嘅次方,仲會變成獨一無二嘅表達方法!😎例如:
12 = 2² x 3¹
30 = 2¹ x 3¹ x 5¹
90 = 2¹ x 3² x 5¹
試諗吓,如果你包埋1落去,咁就失去咗獨一無二嘅特性,因為1嘅幾多次方都係1⋯⋯例如:
21 = 1¹ x 3¹ x 7¹
21 = 1² x 3¹ x 7¹
21 = 1³ x 3¹ x 7¹
……
其實「21 = 1嘅999999次方 x 3 x 7」都得,咁就嘥曬啦!
有咩嘥咗?獨一無二嘅特性代表你可以用嚟編碼呀!用返上面嘅12、30、90做例子,你可以:
用21代表12
用111代表30
用121代表90
(我唔講點解,你估唔估到?)
考吓你,根據呢個編碼方法,請問301001代表幾多?歡迎作答👇🏻
---------
#M1解題王 會以題目 keyword 切入,同你極速 KO M1 題目;記住 Save 低個 post,方便你大考前攞出嚟溫🔥
------------
🎲賭Sir|高階數學考試專家
🎓21 項數學公開試.以一 Take 過考取完美戰績
DSE:Math+M1+M2【5**】
CE & AL:Math+A.Math+Pure+Applied【A】
IAL:C12+C34+F1+F2+F3+M1+M2+M3+S1+S2+S3+D1【A】
IGCSE:Mathematics+Further Pure Mathematics 【9】
🖥最高人氣補習網紅・貼地教數別樹一格
頻道 #杜氏數學 2016年創辦,訂閱65,000+,多條教學影片點擊100,000+;2018年獲出版社邀請,撰寫暢銷書《5**數學男人嫁得過》推廣「聰明應試」理念,並鼓勵年青人堅守自信。
🧠以心理學、高效學習融入補習教育當中
從中文大學風險管理學士畢業之後,鑽研超速學習法(Ultralearning)及教育心理學,將高效學方法先行用於自己身上,無間斷學習新知識;四年後重返校園,完成中文大學數學碩士(大數據分析)課程,期間考入門薩學會(Mensa),實證超速學習法。
🏆座右銘
好多人以為自己因為對數學無興趣,所以數學低分;事實剛好相反:因為自己數學低分,所以對數學無興趣。試諗下,若然你有歌神嘅聲線,你仲會對唱歌無興趣嗎?
------------
#數學 #DSE #dser #math #maths #afterschool #dsemath #examskills #mathtutor #followme #2022DSE #2023DSE #2024DSE #tutor #mathtutor #DSEfighter #tutotial
number教學 在 Ah LAI Channel Youtube 的最佳解答
number教學 在 YOYOTV Youtube 的最佳解答
看更多阿迪牯!遊客記►►https://www.youtube.com/playlist?list=PLo_HY74ov2N2AgkJO0o6YlyrA2Qns18v-
節目大綱:好客森林裡的精靈朋友們,正要去學校上課,精靈朋友熱情的帶著阿迪牯一起進去學校,但在這裡的學校要帶什麼呢?
客語教學句:1.仰結煞?2.去學校你會帶麼个?3.𠊎會帶筆/書包仔/字簿仔/零嗒
客語童謠:歌名:快樂背包客
快樂背包客
一个背包 兩个背包
三个背包 客
你兜大家 背等背包
到底愛去 哪
行行行行行 愛去遶山花係無
Number One 背包客
背等背包向前行
係阿爸去做事 為佢加加油
Number Two 背包客
背背包騎自行車
係阿姊去上課
看着同學Say Hello
一个背包 兩个背包
三个背包 客
你兜大家 背等背包
到底愛去 哪
行行行行行 愛去遶山花係無
Number Three 背包客
背等背包坐公車
係阿姆去尋阿姨
大包小包包一包
細細个 背包客
背等背包跈等行
係Baby 挷衫尾
背包客又多一儕
Number One 背包客
背等背包向前行
Number Two 背包客
背背包騎自行車
Number Three 背包客
背等背包坐公車
細細个 背包客
背等背包跈等行
一个背包 兩个背包
三个背包 客
加上盡尾一个
就有四个背包 客
華文翻譯
快樂背包客
一個背包 兩個背包
三個背包 客 今天你們 背著背包
到底要去 哪兒 走走走走走 是不是要去郊遊
Number One 背包客
背著背包向前走
是爸比去工作 打氣加加油
Number Two 背包客
背著背包騎單車
是姐姐去上課
看到同學Say Hello
一個背包 兩個背包
三個背包 客 今天你們 背著背包
到底要去 哪兒
走走走走走 是不是要去郊遊
Number Three 背包客
背著背包坐公車 是媽咪去找阿姨
大包小包包一包
小小的 背包客
背著背包跟著走 是Baby 愛對路
背包客又多一個
Number One 背包客
背著背包向前走
Number Two 背包客
背著背包騎單車
Number Three 背包客
背著背包坐公車
小小的 背包客
背著背包愛對路
一個背包 兩個背包
三個背包 客
加上最後一個
就有四個背包客
number教學 在 Aotter Girls: Girl's Tech Talk Youtube 的最佳貼文
上一次介紹最新款 Apple Watch S6 跟 SE 三大特色比較跟使用續航心得
這次則是整理了過去獺友們的提問跟 Emma 的選購建議!
跟大家分享 6 個你想買或換新款 Apple Watch 前該知道的事情囉
【製作團隊】
企劃:Emma
腳本:Emma
攝影:辣導
剪輯:辣導
字幕:辣導
監製:蜜柑、宇恩、Emma
✨快加入我們的會員:https://supr.link/zZE8x
【訂閱電獺少女 YouTube】 https://supr.link/o3WBV
【追蹤電獺少女 Instagram】 https://supr.link/nYIMY
【按讚電獺少女 Facebook】 https://supr.link/VAZd6
【電獺少女官方網站】 https://supr.link/AKiW8
🔥 熱門影片 🔥
iPad Pro (2020) 開箱!買了它就不用買 MacBook Air 了嗎?
👉 https://supr.link/01OOf
iPhone 備忘錄六大必知小技巧!超實用功能你會嗎?
👉 https://supr.link/e5vdr
交友軟體能交友嗎?女孩覺得哪款 App 最好用?
👉 https://supr.link/WDk9c
13 副熱門真無線耳機大亂鬥!AirPods Pro、Sennheiser、Sony...該挑哪副?
👉 https://supr.link/MmBj9
ROG Phone 3 電競手機上手!螢幕超狂規格,二三代配件可通用
👉 https://supr.link/5lfGe
※更多開箱影片 👉 https://supr.link/hEERu
※更多 iPhone 相關影片 👉 https://supr.link/AzR5u
※更多 Android 手機 👉 https://supr.link/0K9Co
※教學小技巧大公開 👉 https://supr.link/lfyZk
※熱門藍牙耳機這邊找 👉 https://supr.link/SdDPr
#AppleWatch #Apple #蘋果手錶
number教學 在 Excel教學- Cells儲存格stored as text/number - Advanced Level... 的推薦與評價
Cells儲存格stored as text/number - Advanced Level 上次講述到1個非常重要的概念,就係儲存格內只可能是文字或數字,但數字亦可以是文字,簡單舉例大家就會易明白依個 ... ... <看更多>