From dab16ee53aaad94a146c625958e707dcc54ef96d Mon Sep 17 00:00:00 2001 From: weibk <79395818+weibk@users.noreply.github.com> Date: Mon, 16 Jan 2023 18:12:38 +0800 Subject: [PATCH] update python randomAccess MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit random.randomint(start, stop) 返回 数字的范围是 [start, stop] 是一个闭区间, 如果用len(nums) 有可能会索引超出范围 --- docs/chapter_array_and_linkedlist/array.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index fcbdf9ab2..b18a0bbd9 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -137,7 +137,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex """ 随机访问元素 """ def randomAccess(nums): # 在区间 [0, len(nums)) 中随机抽取一个数字 - random_index = random.randint(0, len(nums)) + random_index = random.randint(0, len(nums)-1) # 获取并返回随机元素 random_num = nums[random_index] return random_num