📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有1部Youtube影片,追蹤數超過2萬的網紅數學老師張旭,也在其Youtube影片中提到,【摘要】 本影片承接上回許願池影片,講解連續變數的機率分布,包含均勻分布、指數分布、常態分布、Gamma 分布和 Beta 分布及他們的機率密度函數與期望值和變異數 【加入會員】 歡迎加入張旭老師頻道會員 付費定閱支持張旭老師,讓張旭老師能夠拍更多的教學影片 https://www.youtube...
「uniform continuous」的推薦目錄:
- 關於uniform continuous 在 Taipei Ethereum Meetup Facebook 的最佳貼文
- 關於uniform continuous 在 Daphne Iking Facebook 的最佳解答
- 關於uniform continuous 在 JJ Acuna / Bespoke Studio Facebook 的最佳貼文
- 關於uniform continuous 在 數學老師張旭 Youtube 的最讚貼文
- 關於uniform continuous 在 Difference between continuity and uniform continuity - Math ... 的評價
- 關於uniform continuous 在 Plotting CDF of Uniform continuous distribution - Stack Overflow 的評價
uniform continuous 在 Daphne Iking Facebook 的最佳解答
The ‘Back-to-School’ programme helps underprivileged and urban poor primary school children nationwide by providing school backpacks that consist of a pair of school uniform, a pair of school shoes, and socks.
This programme is an initiative by McDonald’s Malaysia and Ronald McDonald House Charities (RMHC) in collaboration with the Ministry of Education to motivate primary school children to kick start the new 2020 school year on high spirits!
This year, McDonald’s and RMHC will be reaching out to 30,000 primary school children across the country.
We too can help the underprivileged children kick-start the new school year, by donating towards RMHC Malaysia through the donation box at any McDonald’s restaurant or via an online transfer at https://rmhc-malaysia.my/.
Your contribution will help alleviate these families’ financial burdens and at the same time, motivate the children to start school. Kudos to McDonald’s and RMHC Malaysia for your continuous efforts in bringing cheer to children in need nationwide through all your community programme engagements, such as the ‘Back-to-School’ programme.
Watch this video to find out more about the ‘Back-to-School’ programme: https://www.youtube.com/watch?v=qrkcBkTZ0us&feature=youtu.be
#mcdbacktoschool
#mcdkembalikesekolah
#komunitimcdrmhc
uniform continuous 在 JJ Acuna / Bespoke Studio Facebook 的最佳貼文
In The News: via F&B Report, December 2018
“Designing A Rebranding Strategy”, an article by @ericatlass for F&B Report about Tokyo Tokyo’ recent transformation, emphasizes the need for continuous brand reviews and investment into your brand image. When I found out I was assigned to photograph the restaurant for the magazine, I was thrilled. Their first Lifestyle Store in TriNoma is exciting and with a more elevated ambience (as their VP for marketing @leeperez25 mentions in the article) with the help of TheWanderlister+ of JJ Acuna / Bespoke Studio. Swipe to see some of the menu items you can only get at this specific branch of Tokyo Tokyo. Pick up the latest issue to see the full feature! #interiors #restaurantdesign #uniform #food #東京東京 #interiordesign #design #philippines #interiorphotography #foodphotography #fnbreport #sushi @ Tokyo Tokyo, Trinoma photos by Jar Concengco
uniform continuous 在 數學老師張旭 Youtube 的最讚貼文
【摘要】
本影片承接上回許願池影片,講解連續變數的機率分布,包含均勻分布、指數分布、常態分布、Gamma 分布和 Beta 分布及他們的機率密度函數與期望值和變異數
【加入會員】
歡迎加入張旭老師頻道會員
付費定閱支持張旭老師,讓張旭老師能夠拍更多的教學影片
https://www.youtube.com/channel/UCxBv4eDVLoj5XlRKM4iWj9g/join
【會員等級說明】
博士等級:75 元 / 月
- 支持我們拍攝更多教學影片
- 可在 YT 影片留言處或聊天室使用專屬貼圖
- 你的 YT 名稱前面會有專屬會員徽章
- 可觀看會員專屬影片 (張旭老師真實人生挑戰、許願池影片)
- 可加入張旭老師 YT 會員專屬 DC 群
碩士等級:300 元 / 月
- 享有博士等級所有福利
- 每個月可問 6 題高中或大學的數學問題 (沒問完可累積)
學士等級:750 元 / 月
- 享有博士等級所有福利
- 每個月可問 15 題高中或大學的數學問題 (沒問完可累積)
- 可許願希望我們拍攝講解的主題 (高中、大學數學)
- 可免費參加張旭老師線上考衝班 (名額不可轉讓)
家長會等級:1600 元 / 月
- 享有博士等級所有福利
- 沒有解題服務,如需要,得另外購入點數換取服務
- 可許願希望我們拍攝講解的主題 (高中、大學數學)
- 可免費參加張旭老師線上考衝班 (名額可轉讓)
- 可參與頻道經營方案討論
- 可免費獲得張旭老師實體產品
- 可以優惠價報名參加張旭老師所舉辦之活動
股東會等級:3200 元 / 月
- 享有家長會等級所有福利
- 一樣沒有解題服務,如需要,得另外購入點數換取服務
- 本頻道要募資時擁有優先入股權
- 可加入張旭老師商業結盟
- 可參加商業結盟餐會
- 繳滿六個月成為終生會員,之後可解除自動匯款
- 終生會員只需要餐會費用即可持續參加餐會
【勘誤】
無,有任何錯誤歡迎留言告知
【習題】
無
【講義】
無
【附註】
本系列影片僅限 YouTube 會員優先觀看
非會員僅開放「單數集」影片
若想看到所有許願池影片
請加入數學老師張旭 YouTube 會員
加入會員連結 👉 https://reurl.cc/Kj3x7m
【張旭的話】
你好,我是張旭老師
這是我為本頻道會員所專門拍攝的許願池影片
如果你喜歡我的教學影片
歡迎訂閱我的頻道🔔,按讚我的影片👍
並幫我分享給更多正在學大學數學的同學們,謝謝
【學習地圖】
EP01:向量微積分重點整理 (https://youtu.be/x9Z23o_Z5sQ)
EP02:泰勒展開式說明與應用 (https://youtu.be/SByv7fMtMTY)
EP03:級數審斂法統整與習題 (https://youtu.be/qXCdZF8CV7o)
EP04:積分技巧統整 (https://youtu.be/Ioxd9eh6ogE)
EP05:極座標統整與應用 (https://youtu.be/ksy3siNDzH0)
EP06:極限嚴格定義題型 + 讀書方法分享 (https://youtu.be/9ItI09GTtNQ)
EP07:常見的一階微分方程題型及解法 (https://youtu.be/I8CJhA6COjk)
EP08:重製中
EP09:反函數定理與隱函數定理 (https://youtu.be/9CPpcIVLz7c)
EP10:多變數求極值與 Lagrange 乘子法 (https://youtu.be/XsOmQOTzdSA)
EP11:Laplace 轉換 (https://youtu.be/GZRWgcY5i6Y)
EP12:Fourier 級數與 Fourier 轉換 (https://youtu.be/85q-2nInw7Y)
EP13:換變數定理與 Jacobian 行列式 (https://youtu.be/7z4ad1I0b7o)
EP14:Cayley-Hamilton 定理 & 極小多項式 (https://youtu.be/9c-lCLV4F0M)
EP15:極限、微分和積分次序交換的條件 (https://youtu.be/QRkGLK7Iw4c)
EP16:機率密度函數 (上) (https://youtu.be/PR1NSAOP_Z0)
EP17:機率密度函數 (下) 👈 目前在這裡
持續更新中...
【版權宣告】
本影片版權為張旭 (張舜為) 老師所有
嚴禁用於任何商業用途⛔
如果有學校老師在課堂使用我的影片的話
請透過以下聯絡方式通知我讓我知道,謝謝
【張旭老師其他頻道或社群平台】
FB:https://www.facebook.com/changhsu.math
IG:https://www.instagram.com/changhsu.math
Twitch:https://www.twitch.tv/changhsu_math
Bilibili:https://space.bilibili.com/521685904
【其他贊助管道】
歐付寶:https://payment.opay.tw/Broadcaster/Donate/E1FDE508D6051EA8425A8483ED27DB5F (台灣境內用這個)
綠界:https://p.ecpay.com.tw/B3A1E (台灣境外用這個)
#連續型機率分布 #機率密度函數 #pdf
uniform continuous 在 Plotting CDF of Uniform continuous distribution - Stack Overflow 的推薦與評價
... <看更多>
uniform continuous 在 Difference between continuity and uniform continuity - Math ... 的推薦與評價
Definition 4.4.5. A function f:A→R is uniformly continuous ... ... <看更多>
相關內容