php隨機生成車牌號,生成汽車牌照

用戶隨機50選1。好的車牌用戶選不到。

我目前的做法是這樣的。所有車牌入庫。別人選了狀態就修改為1。下面是入庫程序,想跟大家討論一下,有沒有更好的方式。

use Illuminate\Database\Seeder;

class LicensePlatesTableSeeder extends Seeder

{

public function identicalNumber($numbers = [])

{

$identical = array_count_values($numbers);

$data = [

1 => 0,

2 => 0,

3 => 0,

4 => 0,

5 => 0,

];

foreach ($identical as $item) {

$data[$item]++;

}

return $data;

}

public function orderLength($keys = [])

{

$current = null;

$length = 0;

foreach ($keys as $key) {

if ($current === null || $key - 1 != $current || $key==26) {

$current = $key;

$length = 1;

continue;

}

$length += 1;

}

return $length;

}

public function run()

{

/**

* 生成26個字母和0到9的數字

*/

$data = [];

for ($i = 65; $i < 91; $i++) {

$data[] = strtoupper(chr($i));

}

for ($i = 0; $i <= 9; $i++) {

$data[] = $i;

}

$license = [];

foreach ($data as $key1 => $item1) {

foreach ($data as $key2 => $item2) {

foreach ($data as $key3 => $item3) {

foreach ($data as $key4 => $item4) {

foreach ($data as $key5 => $item5) {

$numbers = [$item1, $item2, $item3, $item4, $item5];

$key = [$key1, $key2, $key3, $key4, $key5];

$identicalNumber = $this->identicalNumber($numbers);

$license[] = [

'number' => implode('', $numbers),

'order_length' => $this->orderLength($key),//最大順子長度。

'reverse_order_length' => $this->orderLength(array_reverse($key)),//倒序,最大順子長度。

'identical_2' => $identicalNumber[2],//有2個數字相同的次數。

'identical_3' => $identicalNumber[3],//有3個數字相同的次數。

'identical_4' => $identicalNumber[4],//有4個數字相同的次數。

'identical_5' => $identicalNumber[5],//有5個數字相同的次數。

];

if (count($license) >= 8000) {

\App\LicensePlate::insert($license);

$license = [];

}

}

}

}

}

}

if ($license) {

\App\LicensePlate::insert($license);

}

\App\LicensePlate::where('id', '>', 0)->update([

'created_at' => \Carbon\Carbon::now()->toDateTimeString(),

'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),

]);

}

}

會生成6千萬+條數據。用戶查詢出問題。之前想過,好的車牌放一個表,差的放一個表,但是怕業務會改規則,可能后面3個相同的,或4個相同的,都放出來隨機。

用戶查詢50個隨機車牌,去掉一些好的車牌。大家有什么好的優化方式嗎?

$id=mt_rand(1,36*36*36*36*36-100000);

$LicensePlates = App\LicensePlate::select('id', 'number')

->whereBetween('id',[$id,$id+100000])//為什么加這個,是因為不加根本查不出來

->whereNull('use_at')//已使用的

->where('order_length', '<=', 2)//不能選順子長度大于等于3的 比如 123AD ABCD8

->where('reverse_order_length', '<=', 2)//不能選倒序順子長度大于等于3的 比如 AD321 D4321

->where('identical_2', '<=', 1)//不能選有二個對子,比如 11A22,BBC88

->where('identical_3', 0)//不能選2個相同的 比如 A1A4A AA23A AAA23

->where('identical_4', 0)//不能選4個相同的 比如 AAAA4 77B77 777B7

->where('identical_5', 0)//不能選5個相同的 比如 88888 99999 AAAAA

->inRandomOrder()//隨機

->limit(50)

->get();

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392263.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392263.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392263.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Go_ go mod 命令解決墻的問題

簡介 由于眾所周知的原因&#xff0c;在下載一些庫的時候會下載不了&#xff0c;比如 golang.org/x/... 相關的庫。為此&#xff0c;網上出現了很多解決方案。 從 Go1.11 開始&#xff0c;Go 引入了 module&#xff0c;對包進行管理&#xff0c;通過 go mod 命令來進行相關操作…

leetcode 1319. 連通網絡的操作次數(并查集)

用以太網線纜將 n 臺計算機連接成一個網絡&#xff0c;計算機的編號從 0 到 n-1。線纜用 connections 表示&#xff0c;其中 connections[i] [a, b] 連接了計算機 a 和 b。 網絡中的任何一臺計算機都可以通過網絡直接或者間接訪問同一個網絡中其他任意一臺計算機。 給你這個…

MySQL中choose標簽的用法

先給大家來個SQL語句&#xff1a; choose (when,otherwize) ,相當于java 語言中的 switch ,與 jstl 中 的 choose 很類似。 <select id"getMemberInfo" resultType"java.util.Map" parameterType"java.util.Map" > SELECT …

php hsetnx,HSETNX命令_視頻講解_用法示例-redis編程詞典-php中文網

set英 [set] 美 [s?t]vt.設置;放置&#xff0c;安置;使處于某種狀況;擺放餐具vi.落山;出發;凝結n.集合;一套&#xff0c;一副;布景;電視機adj.固定的;位于…的;頑固的;安排好的第三人稱單數&#xff1a; sets 復數&#xff1a; sets 現在分詞&#xff1a; setting 過去式&am…

用導函數的圖像判斷原函數的單調性

前言 典例剖析 例1(給定\(f(x)\)的圖像&#xff0c;確定\(f(x)\)的單調性&#xff0c;最簡單層次) 題目暫略。 例2(用圖像確定\(f(x)\)的正負&#xff0c;確定\(f(x)\)的單調性&#xff0c;2017聊城模擬) 已知函數\(yxf(x)\)的圖像如圖所示(其中\(f(x)\)是函數\(f(x)\)的導函數…

樸素貝葉斯 半樸素貝葉斯_使用樸素貝葉斯和N-Gram的Twitter情緒分析

樸素貝葉斯 半樸素貝葉斯In this article, we’ll show you how to classify a tweet into either positive or negative, using two famous machine learning algorithms: Naive Bayes and N-Gram.在本文中&#xff0c;我們將向您展示如何使用兩種著名的機器學習算法&#xff…

python3:面向對象(多態和繼承、方法重載及模塊)

1、多態 同一個方法在不同的類中最終呈現出不同的效果&#xff0c;即為多態。 class Triangle:def __init__(self,width,height):self.width widthself.height heightdef getArea(self):areaself.width* self.height / 2return areaclass Square:def __init__(self,size):sel…

蠕變斷裂 ansys_如何避免范圍蠕變,以及其他軟件設計課程的辛苦學習方法

蠕變斷裂 ansysby Dror Berel由Dror Berel 如何避免范圍蠕變&#xff0c;以及其他軟件設計課程的辛苦學習方法 (How to avoid scope creep, and other software design lessons learned the hard way) 從數據科學的角度來看。 (From a data-science perspective.) You’ve got…

leetcode 674. 最長連續遞增序列

給定一個未經排序的整數數組&#xff0c;找到最長且 連續遞增的子序列&#xff0c;并返回該序列的長度。 連續遞增的子序列 可以由兩個下標 l 和 r&#xff08;l < r&#xff09;確定&#xff0c;如果對于每個 l < i < r&#xff0c;都有 nums[i] < nums[i 1] &a…

深入單例模式 java,深入單例模式四

Java代碼 privatestaticClass getClass(String classname)throwsClassNotFoundException {ClassLoader classLoader Thread.currentThread().getContextClassLoader();if(classLoader null)classLoader Singleton.class.getClassLoader();return(classLoader.loadClass(class…

linux下配置SS5(SOCK5)代理服務

SOCK5代理服務器 官網: http://ss5.sourceforge.net/ yum -y install gcc gcc-c automake make pam-devel openldap-devel cyrus-sasl-devel 一、安裝 # tar xvf ss5-3.8.9-5.tar.gz # cd ss5-3.8.9-5 # ./configure && make && make install 二、修改配置文…

劉備和諸葛亮鬧翻:無意說出蜀國滅亡的根源?

導讀&#xff1a;身為管理者&#xff0c;一件事情&#xff0c;自己做是滿分&#xff0c;別人做是八十分&#xff0c;寧可讓人去做八十分&#xff0c;自己也得跳出來看全局。緊抓大權不放&#xff0c;要么自己干到死&#xff0c;要么是敗于戰略&#xff01;&#xff01; 諸葛亮去…

mysql 時間推移_隨著時間的推移可視化COVID-19新案例

mysql 時間推移This heat map shows the progression of the COVID-19 pandemic in the United States over time. The map is read from left to right, and color coded to show the relative numbers of new cases by state, adjusted for population.該熱圖顯示了美國COVID…

leetcode 959. 由斜杠劃分區域(并查集)

在由 1 x 1 方格組成的 N x N 網格 grid 中&#xff0c;每個 1 x 1 方塊由 /、\ 或空格構成。這些字符會將方塊劃分為一些共邊的區域。 &#xff08;請注意&#xff0c;反斜杠字符是轉義的&#xff0c;因此 \ 用 “\” 表示。&#xff09;。 返回區域的數目。 示例 1&#x…

rcu寬限期_如何處理寬限期錯誤:靜默失敗不是一種選擇

rcu寬限期by Rina Artstain通過麗娜阿斯特斯坦 I’ve never really had much of an opinion about error handling. This may come as a shock to people who know me as quite opinionated (in a good way!), but yeah. If I was coming into an existing code base I just d…

描述符、迭代器、生成器

描述符&#xff1a;將某種特殊類型的類的實例指派給另一個類的屬性。 此處特殊類型的要求&#xff0c;至少實現”__set__(self , instance , owner)“、”__get__(self , instance , value)“、”__delete__(self , instance )“三個方法中的一個。 >>> class MyDecri…

php模擬表單提交登錄,PHP模擬表單的post請求實現登錄

stuid > $stuid,pwd > $pwd);$ch curl_init (); //初始化curlcurl_setopt ( $ch, CURLOPT_URL, $uri );curl_setopt ( $ch, CURLOPT_POST, 1 ); //使用post請求curl_setopt ( $ch, CURLOPT_HEADER, 0 );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt ( $…

去除list集合中重復項的幾種方法

因為用到list&#xff0c;要去除重復數據&#xff0c;嘗試了幾種方法。記錄于此。。。 測試數據&#xff1a; List<string> li1 new List<string> { "8", "8", "9", "9" ,"0","9"};List<string&g…

Crystal Reports第一張報表

新建一個網站項目&#xff0c;1. 設置數據庫 從服務器資源管理器中&#xff0c;數據連接中添加新連接&#xff0c;用Microsoft Access數據庫文件作為數據提供程序&#xff0c;連接上Crystal Reports的用例的數據庫Xtreme2. 創建新Crystal Reports報表 在工程項目中添加一個…

leetcode 1128. 等價多米諾骨牌對的數量

給你一個由一些多米諾骨牌組成的列表 dominoes。 如果其中某一張多米諾骨牌可以通過旋轉 0 度或 180 度得到另一張多米諾骨牌&#xff0c;我們就認為這兩張牌是等價的。 形式上&#xff0c;dominoes[i] [a, b] 和 dominoes[j] [c, d] 等價的前提是 ac 且 bd&#xff0c;或是…