Home php教程 php手册 浅谈php随机不重复数的两种算法

浅谈php随机不重复数的两种算法

Jun 06, 2016 pm 07:50 PM
php plural introduce algorithm random

先引入别人的一个秒表计时类(counttime.class.php): 1.先看第一个例子(大数中取少数): 1 ? 2 /* 3 @描述: Stopwatch这个类用户获取脚本执行时间 4 @作者: Klesti Hoxha klesti@gmail.com 5 */ 6 7 class Stopwatch { 8 private $start ; 9 private $en

先引入别人的一个秒表计时类(counttime.class.php):

1.先看第一个例子(大数中取少数):

<span> 1</span> 
<span> 2</span> <span>/*</span>
<span> 3</span> <span>@描述: Stopwatch这个类用户获取脚本执行时间
</span><span> 4</span> <span>@作者: Klesti Hoxha <klesti>
</klesti></span><span> 5</span> <span>*/</span>
<span> 6</span> 
<span> 7</span> <span>class</span><span> Stopwatch {
</span><span> 8</span> <span>private</span> <span>$start</span><span>;
</span><span> 9</span> <span>private</span> <span>$end</span><span>;
</span><span>10</span> <span>private</span> <span>$markup_start</span> = <span>array</span><span>();
</span><span>11</span> <span>private</span> <span>$markup_end</span> = <span>array</span><span>();
</span><span>12</span> 
<span>13</span> <span>function</span> __construct(<span>$markup</span>=<span>false</span><span>) {
</span><span>14</span> <span>$this</span>->start(<span>$markup</span><span>);
</span><span>15</span> <span>}
</span><span>16</span> 
<span>17</span> <span>public</span> <span>function</span> start(<span>$markup</span>=<span>false</span><span>) {
</span><span>18</span> <span>if</span> (!<span>$markup</span><span>) {
</span><span>19</span> <span>$this</span>->start = <span>$this</span>-><span>getmicrotime();
</span><span>20</span> } <span>else</span><span> {
</span><span>21</span> <span>$this</span>->markup_start[<span>$markup</span>] = <span>$this</span>-><span>getmicrotime();
</span><span>22</span> <span>}
</span><span>23</span> <span>}
</span><span>24</span> 
<span>25</span> <span>public</span> <span>function</span> stop(<span>$markup</span>=<span>false</span><span>) {
</span><span>26</span> <span>if</span> (!<span>$markup</span><span>) {
</span><span>27</span> <span>$this</span>-><span>end</span> = <span>$this</span>-><span>getmicrotime();
</span><span>28</span> } <span>else</span><span> {
</span><span>29</span> <span>$this</span>->markup_end[<span>$markup</span>] = <span>$this</span>-><span>getmicrotime();
</span><span>30</span> <span>}
</span><span>31</span> <span>return</span> <span>$this</span>->getDuration(<span>$markup</span><span>);
</span><span>32</span> <span>}
</span><span>33</span> 
<span>34</span> <span>public</span> <span>function</span> getDuration(<span>$markup</span>=<span>false</span><span>) {
</span><span>35</span>     <span>if</span> (!<span>$markup</span><span>)
</span><span>36</span> <span>     {
</span><span>37</span>         <span>return</span> <span>number_format</span>(<span>$this</span>-><span>end</span>-<span>$this</span>->start,4<span>);
</span><span>38</span>     } <span>else</span><span> {
</span><span>39</span>         <span>return</span> <span>number_format</span>(<span>$this</span>->markup_end[<span>$markup</span>]-<span>$this</span>->markup_start[<span>$markup</span>],4<span>);
</span><span>40</span> <span>    }
</span><span>41</span> <span>}
</span><span>42</span> 
<span>43</span> <span>public</span> <span>function</span> <span>reset</span>(<span>$markup</span><span>) {
</span><span>44</span> <span>if</span> (!<span>$markup</span><span>) {
</span><span>45</span> <span>$this</span>->start = 0<span>;
</span><span>46</span> <span>$this</span>-><span>end</span> = 0<span>;
</span><span>47</span> <span>$this</span>->markup_start = <span>array</span><span>();
</span><span>48</span> <span>$this</span>->markup_end = <span>array</span><span>();
</span><span>49</span> } <span>else</span><span> {
</span><span>50</span> <span>$this</span>->markup_start[<span>$markup</span>] = 0<span>;
</span><span>51</span> <span>$this</span>->markup_end[<span>$markup</span>] = 0<span>;
</span><span>52</span> <span>}
</span><span>53</span> <span>}
</span><span>54</span> 
<span>55</span> <span>private</span> <span>function</span><span> getmicrotime(){
</span><span>56</span> <span>list</span>(<span>$usec</span>, <span>$sec</span>) = <span>explode</span>(" ",<span>microtime</span><span>());
</span><span>57</span> <span>return</span> ((<span>float</span>)<span>$usec</span> + (<span>float</span>)<span>$sec</span><span>);
</span><span>58</span> <span>}
</span><span>59</span> 
<span>60</span> <span>}
</span><span>61</span> ?>
Copy after login

测试的两种方法如下:

<span> 1</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 2</span> <span>php
</span><span> 3</span> <span>require</span> "counttime.class.php";<span>//</span><span>把类文件引用进来,根据你的实际情况来确定路径,这里是在同级目录</span>
<span> 4</span> <span>$s</span> = <span>new</span><span> Stopwatch();
</span><span> 5</span> 
<span> 6</span> <span>$s</span>->start("div1"<span>);
</span><span> 7</span> <span>//</span><span>range 是将1到100 列成一个数组</span>
<span> 8</span> <span>$numbers</span> = <span>range</span> (1,1000000<span>);
</span><span> 9</span> <span>//</span><span>shuffle 将数组顺序随即打乱</span>
<span>10</span> <span>shuffle</span> (<span>$numbers</span><span>);
</span><span>11</span> <span>//</span><span>array_slice 取该数组中的某一段</span>
<span>12</span> <span>$no</span>=30<span>;
</span><span>13</span> <span>$result</span> = <span>array_slice</span>(<span>$numbers</span>,0,<span>$no</span><span>);
</span><span>14</span> <span>for</span> (<span>$i</span>=0;<span>$i</span>$no;<span>$i</span>++<span>){
</span><span>15</span> <span>echo</span> <span>$result</span>[<span>$i</span>]."<br>"<span>;
</span><span>16</span> <span>}
</span><span>17</span> <span>echo</span> "第一个测验方法结束<br><br>"<span>;
</span><span>18</span> <span>$s</span>->stop("div1"<span>);
</span><span>19</span> 
<span>20</span> <span>$s</span>->start("div2"<span>);
</span><span>21</span> <span>function</span> unique_rand(<span>$min</span>, <span>$max</span>, <span>$num</span><span>)
</span><span>22</span> <span>{
</span><span>23</span>         <span>$count</span> = 0<span>;
</span><span>24</span>         <span>$return</span> = <span>array</span><span>();
</span><span>25</span>         <span>while</span> (<span>$count</span> $num<span>) {
</span><span>26</span>             <span>$return</span>[] = <span>mt_rand</span>(<span>$min</span>, <span>$max</span><span>);
</span><span>27</span>             <span>$return</span> = <span>array_flip</span>(<span>array_flip</span>(<span>$return</span><span>));
</span><span>28</span>             <span>$count</span> = <span>count</span>(<span>$return</span><span>);
</span><span>29</span> <span>        }
</span><span>30</span>         <span>shuffle</span>(<span>$return</span><span>);
</span><span>31</span>         <span>return</span> <span>$return</span><span>;
</span><span>32</span> <span>}
</span><span>33</span> 
<span>34</span>     <span>$arr</span> = unique_rand(1, 100000000000, 30<span>);
</span><span>35</span>     <span>$result</span> = ''<span>;
</span><span>36</span>     <span>for</span>(<span>$i</span>=0; <span>$i</span> count(<span>$arr</span>);<span>$i</span>++<span>)
</span><span>37</span> <span>    {
</span><span>38</span>         <span>$result</span> = <span>$arr</span>[<span>$i</span><span>];
</span><span>39</span>         <span>echo</span> <span>$result</span>."<br>"<span>;
</span><span>40</span> <span>    }
</span><span>41</span>     <span>$s</span>->stop("div2"<span>);
</span><span>42</span> 
<span>43</span> 
<span>44</span> <span>//</span><span>输出时间</span>
<span>45</span> <span>echo</span> "div 1 Duration: " . <span>$s</span>->getDuration("div1") . " seconds.<br>"<span>;
</span><span>46</span> <span>echo</span> "div 2 Duration: " . <span>$s</span>->getDuration("div2") . " seconds.<br>"<span>;
</span><span>47</span> ?>
Copy after login

结果为:

<span> 1</span> 413537
<span> 2</span> 198587
<span> 3</span> 60611
<span> 4</span> 599544
<span> 5</span> 946241
<span> 6</span> 462853
<span> 7</span> 570943
<span> 8</span> 980260
<span> 9</span> 717416
<span>10</span> 969324
<span>11</span> 292403
<span>12</span> 614443
<span>13</span> 769469
<span>14</span> 151951
<span>15</span> 167883
<span>16</span> 308727
<span>17</span> 342706
<span>18</span> 955659
<span>19</span> 112311
<span>20</span> 329008
<span>21</span> 182668
<span>22</span> 330600
<span>23</span> 921966
<span>24</span> 993143
<span>25</span> 369227
<span>26</span> 747066
<span>27</span> 793185
<span>28</span> 347872
<span>29</span> 439016
<span>30</span> 428600
<span>31</span> <span>第一个测验方法结束
</span><span>32</span> 
<span>33</span> 649589727
<span>34</span> 450823813
<span>35</span> 324717181
<span>36</span> 44553762
<span>37</span> 375669657
<span>38</span> 37128612
<span>39</span> 25135327
<span>40</span> 468454185
<span>41</span> 73395357
<span>42</span> 753960006
<span>43</span> 214455797
<span>44</span> 5367614
<span>45</span> 1153263961
<span>46</span> 17803441
<span>47</span> 669057198
<span>48</span> 55366324
<span>49</span> 1117343950
<span>50</span> 203530485
<span>51</span> 1160930064
<span>52</span> 326487914
<span>53</span> 565471236
<span>54</span> 7068443
<span>55</span> 846955001
<span>56</span> 832037489
<span>57</span> 1067813034
<span>58</span> 219239595
<span>59</span> 1069795964
<span>60</span> 1049898846
<span>61</span> 926301266
<span>62</span> 197341539
<span>63</span> div 1 Duration: 0.5800 seconds.
<span>64</span> div 2 Duration: 0.0000 seconds.
Copy after login

 

结论:对于从大量数据中取少量数据,显然第二种比较快。

 

2.在来看看从大数中取大数:

<span> 1</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 2</span> <span>php
</span><span> 3</span> <span>require</span> "counttime.class.php";<span>//</span><span>把类文件引用进来,根据你的实际情况来确定路径,这里是在同级目录</span>
<span> 4</span> <span>$s</span> = <span>new</span><span> Stopwatch();
</span><span> 5</span> 
<span> 6</span> <span>$s</span>->start("div1"<span>);
</span><span> 7</span> <span>//</span><span>range 是将1到100 列成一个数组</span>
<span> 8</span> <span>$numbers</span> = <span>range</span> (1,1000000<span>);
</span><span> 9</span> <span>//</span><span>shuffle 将数组顺序随即打乱</span>
<span>10</span> <span>shuffle</span> (<span>$numbers</span><span>);
</span><span>11</span> <span>//</span><span>array_slice 取该数组中的某一段</span>
<span>12</span> <span>$no</span>=10000<span>;
</span><span>13</span> <span>$result</span> = <span>array_slice</span>(<span>$numbers</span>,0,<span>$no</span><span>);
</span><span>14</span> <span>for</span> (<span>$i</span>=0;<span>$i</span>$no;<span>$i</span>++<span>){
</span><span>15</span> <span>echo</span> <span>$result</span>[<span>$i</span>]."<br>"<span>;
</span><span>16</span> <span>}
</span><span>17</span> <span>echo</span> "第一个测验方法结束<br><br>"<span>;
</span><span>18</span> <span>$s</span>->stop("div1"<span>);
</span><span>19</span> 
<span>20</span> <span>$s</span>->start("div2"<span>);
</span><span>21</span> <span>function</span> unique_rand(<span>$min</span>, <span>$max</span>, <span>$num</span><span>)
</span><span>22</span> <span>{
</span><span>23</span>         <span>$count</span> = 0<span>;
</span><span>24</span>         <span>$return</span> = <span>array</span><span>();
</span><span>25</span>         <span>while</span> (<span>$count</span> $num<span>) {
</span><span>26</span>             <span>$return</span>[] = <span>mt_rand</span>(<span>$min</span>, <span>$max</span><span>);
</span><span>27</span>             <span>$return</span> = <span>array_flip</span>(<span>array_flip</span>(<span>$return</span><span>));
</span><span>28</span>             <span>$count</span> = <span>count</span>(<span>$return</span><span>);
</span><span>29</span> <span>        }
</span><span>30</span>         <span>shuffle</span>(<span>$return</span><span>);
</span><span>31</span>         <span>return</span> <span>$return</span><span>;
</span><span>32</span> <span>}
</span><span>33</span> 
<span>34</span>     <span>$arr</span> = unique_rand(1, 1000000, 10000<span>);
</span><span>35</span>     <span>$result</span> = ''<span>;
</span><span>36</span>     <span>for</span>(<span>$i</span>=0; <span>$i</span> count(<span>$arr</span>);<span>$i</span>++<span>)
</span><span>37</span> <span>    {
</span><span>38</span>         <span>$result</span> = <span>$arr</span>[<span>$i</span><span>];
</span><span>39</span>         <span>echo</span> <span>$result</span>."<br>"<span>;
</span><span>40</span> <span>    }
</span><span>41</span>     <span>$s</span>->stop("div2"<span>);
</span><span>42</span> 
<span>43</span> 
<span>44</span> <span>//</span><span>输出时间</span>
<span>45</span> <span>echo</span> "div 1 Duration: " . <span>$s</span>->getDuration("div1") . " seconds.<br>"<span>;
</span><span>46</span> <span>echo</span> "div 2 Duration: " . <span>$s</span>->getDuration("div2") . " seconds.<br>"<span>;
</span><span>47</span> ?>
Copy after login

输出结果为:

<span>1</span> <span>前面省略………………
</span><span>2</span> div 1 Duration: 0.6050 seconds.
<span>3</span> div 2 Duration: 17.4260 seconds.
Copy after login

结论:对于第一种方法输出一个数据的时间,跟大数的数字时间差不多。但是第二个时间显然耗费得要离谱。达到了17.5秒。

 

总结:

1.翻翻法对于选少量数据好用,大量数据就会比较吃力。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles