erlang乱序一个List的方案
最近在系统地看erlang,目前以《erlang programming》为学习材料,看完前面七八章,试图写点东西,发现手足无措,能找到帮助的资料和文档也非常少。
其实我希望有人能一起来学习或讨论。
今日题目: 乱序一个List.
方法一: 一个较为精巧的乱序方案。
[php]
-module(shuffle).
-export([do/1]).
do(L) ->
Len = length(L),
NL = lists:map(fun(X) -> {random:uniform(Len), X} end, L),
NLL = lists:sort(NL),
[ V || {_,V}
其实这个问题挺困扰我的,变量一旦被赋值便不可修改,这个特性让原本写程序的想法和思路完全不同了。我上面的做法是,利用随机数生成一个[{Rand1,Elem1},...,{RandN,ElemN}]这样的列表。
然后再用sort排序后,重新打印出Elem出来,乱序的效果由此达到。
结果大概如下:
[php]
45> c(shuffle).
{ok,shuffle}
46> shuffle:do([1,2,3,4,5]).
[4,5,1,2,3]
47> shuffle:do([1,2,3,4,5]).
[2,1,5,3,4]
48> shuffle:do([1,2,3,4,5]).
[3,1,2,4,5]
49> shuffle:do([1,2,3,4,5]).
[1,5,2,3,4]
50> shuffle:do([1,2,3,4,5]).
[5,1,4,3,2]
51>
方法二:通用洗牌算法
接上文,乱序一个List,我想原有的通用洗牌算法可行的。尝试写了另一个函数。经测试有效,但是我想肯定有优化的空间。代码的风格仍然透露着浓重的过程式思想吧。
%使用洗牌算法的方案
[php]
do2(L) ->
do2(L,[]).
do2([],L) ->
L;
do2(L1,L2) ->
%io:format("L1=~w L2=~w~n",[L1,L2]),
Len = length(L1),
if
Len > 1 ->
NL = lists:split(random:uniform(Len-1), L1),
{[H1|T1],[H2|T2]} = NL,
NL2 = lists:flatten([T1],[H1|T2]),
L11 = lists:append(L2,[H2]),
do2(NL2, L11);
true ->
do2([],lists:append(L2,L1))
end.
其结果为:
128> c(shuffle).
{ok,shuffle}
129> shuffle:do2(lists:seq(0,9)).
[9,2,5,1,8,0,7,3,6,4]
130> shuffle:do2(lists:seq(0,9)).
[8,3,6,5,7,4,9,0,2,1]
131> shuffle:do2(lists:seq(0,9)).
[5,3,7,8,1,9,0,6,2,4]
132> shuffle:do2(lists:seq(0,9)).
[3,0,5,2,1,6,8,4,9,7]
133>
有几点值得思考的,关于这两种方案,是否是真正的洗牌算法。每个数字出现在某个位置是否等概率呢。
还有其它的方法不,欢迎探讨。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

The best version of the Apple 16 system is iOS16.1.4. The best version of the iOS16 system may vary from person to person. The additions and improvements in daily use experience have also been praised by many users. Which version of the Apple 16 system is the best? Answer: iOS16.1.4 The best version of the iOS 16 system may vary from person to person. According to public information, iOS16, launched in 2022, is considered a very stable and performant version, and users are quite satisfied with its overall experience. In addition, the addition of new features and improvements in daily use experience in iOS16 have also been well received by many users. Especially in terms of updated battery life, signal performance and heating control, user feedback has been relatively positive. However, considering iPhone14

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

A computer operating system is a system used to manage computer hardware and software programs. It is also an operating system program developed based on all software systems. Different operating systems have different users. So what are the computer systems? Below, the editor will share with you what computer operating systems are. The so-called operating system is to manage computer hardware and software programs. All software is developed based on operating system programs. In fact, there are many types of operating systems, including those for industrial use, commercial use, and personal use, covering a wide range of applications. Below, the editor will explain to you what computer operating systems are. What computer operating systems are Windows systems? The Windows system is an operating system developed by Microsoft Corporation of the United States. than the most

Detailed explanation of the method of modifying the system date in the Oracle database. In the Oracle database, the method of modifying the system date mainly involves modifying the NLS_DATE_FORMAT parameter and using the SYSDATE function. This article will introduce these two methods and their specific code examples in detail to help readers better understand and master the operation of modifying the system date in the Oracle database. 1. Modify NLS_DATE_FORMAT parameter method NLS_DATE_FORMAT is Oracle data

Linux and Windows are two common operating systems, representing the open source Linux system and the commercial Windows system respectively. In both operating systems, there is a command line interface for users to interact with the operating system. In Linux systems, users use the Shell command line, while in Windows systems, users use the cmd command line. The Shell command line in Linux system is a very powerful tool that can complete almost all system management tasks.
