蒙提霍尔(三门问题)go程序模拟

在某个电视节目比赛环节中,参赛者会看见三扇关闭了的门,其中一扇的后面有一辆汽车,选中后面有车的那扇门可赢得该汽车,另外两扇门后面则各藏有一只山羊。参赛者选定了一扇门,但未开启它,随后节目主持人蒙提霍尔(Monty Hall)开启了剩下的两扇门中的一扇并且发现后面是一只山羊,此时主持人会给予选手重新选择的机会。问题是:此时参赛者换另一扇门会否增加赢得汽车的机率?

package main

import (
	"fmt"
	"math/rand"
	"time"
)

var (
	choose, answer                                   int
	successChange, successKeep, successUnknownChange = 0, 0, 0
)

func main() {
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 10000; i++ {
		choose = rand.Intn(3)
		answer = rand.Intn(3)
		//不换且正确
		if answer == choose {
			successKeep++
		}
		//主持知道答案,排除一个,选择换(第一次选中换后不可能选中)
		if (choose+1)%3 == answer || (choose+2)%3 == answer {
			successChange++
		}
		//主持不知道答案,随机排除,选择换
		newChoose := (choose + rand.Intn(2)) % 3
		if newChoose == answer {
			successUnknownChange++
		}
	}
	fmt.Println(successChange)
	fmt.Println(successKeep)
	fmt.Println(successUnknownChange)
}

运行结果

~/projects/go_test$ go run main.go
6586
3414
3369

mysql导入报错: unknown command ‘\”‘

最近开始使用dbeaver,在从服务器导出线上sql到本地的时候,发现会出现这样的报错,找了半天原因以为是dbeaver的问题,后来直接用mysqldump执行导出,source命令导入,问题还是发生了。

导出

mysqldump -uroot -p xxx >/tmp/test.sql

导入

mysql -uroot

use xxx; source /path/to/sql

这样执行后还是报错

最后发现是导入连接数据库的默认character-set 问题

连接语句改为mysql -uroot –default-character-set=utf8

重新执行source,问题解决

dbeaver 解决方式: 在导入时设置参数–default-character-set=utf8

DateTime Class 使用

DateTime vs DateTimeImmutable

DateTime 使用modify/add/sub 等修改后,本身时间对象也会跟着修改,format输出的就是最新的时间

DateTimeImmutable修改后,本身时间对象不会修改,相关修改方法返回一个新的时间对象,用新的对象进行赋值才会修改原来的对象

修改时间方法

motify(使用方法和date类似, 传入的参数string和date构造string规则相同)

参考: https://www.php.net/manual/en/datetime.formats.relative.php

代码例子:

这里用放款后生成的一个还款通知举例

$returnTime = new DateTimeImmutable($loan['return_time']); //放款时间
$nextMonth = $returnTime->modify('first day of next month'); //下月开始还款
$notifyTime = $nextMonth->modify('+'. $loan['repayment_notify_day'] . ' days'); //还款通知时间
$repaymentTime = $nextMonth->modify('+'. $loan['repayment_last_day'] . ' days'); //最后还款时间
$notifies = [];

//$schedules 是计算出的每月应还利息本金信息
foreach ($schedules as $schedule) {
    $notifies[] = [
        'customer_id' => $loan['customer_id'],
        'loan_id' => $loan['id'],
        'notify_data' => $notifyTime->format('Y-m-d'),
        'repayment_date' => $repaymentTime->format('Y-m-d'),
        'month' => $notifyTime->format('Ym'),
        'capital_amount' => $schedule['capital'],
        'interests_amount' => $schedule['interests'],
        'total_amount' => round($schedule['interests'] + $schedule['capital'], 2),
        'status' => 'wait',
        'index' => $schedule['index'],
        'notify_time' => null,
    ];
    $notifyTime = $notifyTime->modify('+1 month');
    $repaymentTime = $repaymentTime->modify('+1 month');
}

//$notifies 就是生成的每月还款通知的对应信息了 

docker desktop 修改镜像容器存储位置

docker desktop 进入配置页,里面有可以设置存储位置的配置项,不过这个配置对于用windows WSL 的客户端来说并不会生效,每次设置后都会被重新修改回默认项。

要想修改这个存储位置只能修改wsl的存储位置才行

首先查看当前安装的wsl

wsl -l -v

  NAME                   STATE           VERSION
* docker-desktop-data    Running         2
  docker-desktop         Running         2

关闭wsl

wsl --shutdown

导出desktop-data镜像

wsl --export docker-desktop-data "临时存储地址"

取消注册

wsl --unregister docker-desktop-data

导入备份并指定新路径

wsl --import docker-desktop-data "目标存储路径" "备份地址" --version 2

手撸的一个阶段编辑的组件

element-ui + vue2

效果如下

为自己点赞

<template>
  <div class="stage-container">
    <el-popover
      v-for="(stage) in stageList" :key="stage.id"
      width="200"
      trigger="hover"
    >
      <div slot="reference" :class="stageState(stage)" class="stage">
        <div class="stage-title">{{ stage.name }}</div>
        <div :class="stageState(stage)" class="stage-side stage-left"/>
        <div :class="stageState(stage)" class="stage-side stage-right"/>
      </div>
      <div class="label-list">
        <div v-for="(label) in stage.labels" :key="label.id">
          <el-link :underline="false" :type="labelState(label)" class="label" @click="selectLabel(label)">{{ label.name }}</el-link>
        </div>
      </div>
    </el-popover>
  </div>
</template>

<script>
import { customerStages } from '@/api/admin/crm'
export default {
  props: {
    stageId: Number,
    stageLabelId: Number
  },
  data() {
    return {
      stageList: []
    }
  },
  computed: {
    stageState() {
      return stage => {
        if (!this.currentStage.order) {
          return 'not-active'
        }
        return stage.order > this.currentStage.order ? 'not-active' : ''
      }
    },
    labelState() {
      return label => {
        return this.stageLabelId == label.id ? 'primary' : 'info'
      }
    },
    currentStage() {
      for (const stage of this.stageList) {
        if (stage.id == this.stageId) {
          return stage
        }
      }
      return {}
    }
  },
  async mounted() {
    const res = await customerStages()
    this.stageList = res.data
  },
  methods: {
    selectLabel(label) {
      this.$emit('selectLabel', label)
    }
  }
}
</script>

<style lang="scss">
.stage-container {
  margin-left: 50px;
  display: flex;
}

.stage-side {
  height: 38px;
  width: 38px;
  top: 0px;
  position: absolute;
  transform: rotate(45deg) scale(0.707);
  &.stage-left {
    z-index: 5;
    left: -19px;
    background-color: white;
  }
  &.stage-right {
    z-index: 10;
    right: -18px;
    background-color: $xr-color-primary;
  }
  &.not-active {
    background-color: white;
    border-top: 2px solid $xr-color-primary;
    border-right: 2px solid $xr-color-primary;
    top: -2px;
  }
}

.stage {
  color: #fff;
  position: relative;
  padding: 10px 20px 10px 37px;
  margin: 28px 2px;
  height: 38px;
  background-color: $xr-color-primary;
  cursor: pointer;
  &.not-active {
    color: $xr-color-primary;
    background-color: white;
    border-top: 2px solid $xr-color-primary;
    border-bottom: 2px solid $xr-color-primary;
  }
}

.label {
  padding: 5px;
  font-size: 13px;
  width: 100%;
}
</style>

随记

2022-12-28

clash 本地连接错误

修改profile 设置dns false

php版本切换(ubuntu)

安装apt install php8.1 php8.1-fpm
安装apt install php7.4 php7.4-fpm
update-alternatives — config php

systemctl start php7.4-fpm

/etc/nginx/conf.d/test.conf

        location ~ \.php$ {
                fastcgi_pass unix:/run/php/php8.1-fpm.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

宝塔运行指定版本composer

php80 /www/server/php/80/bin/composer 

文档工具

mkdocs

生成随机id

uuid 雪花算法(php-snowflake)

swoole redis连接池应用

遇到问题

有一个cli应用,需要很多协程频繁访问redis,用swoole的redis连接池实现链接;
但是遇到问题,发现内存很快溢出,第一反应是变量没释放,unset了半天没有解决;
然后通过

use Swoole\Coroutine;
$coros = Coroutine::listCoroutines();

定时查看了一下协程数量,发现协程缓慢在增加,慢慢的内存就溢出了。
限制了一下协程数量,扩充了一下连接池大小,挂起的协程数量明显减少了,但还是越来越多,猜测是从redis连接池获取连接的时候获取的连接不可用,然后一直挂起了。

解决方式

从连接池中取出连接后测试一下,可以使用再取出,不然push(null)让连接池生成一个新的,接着继续取,知道连接可用,这样处理后协程数量就一直不会一直增长了。

    protected function redisOperation($callback) {
        $connection = $this->pool->get();
        while(!$connection->ping()) {
            $this->pool->put(null);
            $connection = $this->pool->get();
        }
        $result = call_user_func($callback, $connection);
        $this->pool->put($connection);
        return $result;
    }

问题顺利解决,撒花

git pull/push 速度慢

问题:

windows环境下,不知道为什么最近gitpush和pull速度很慢,经常连接超时;

解决方案:

搜了一下解决方案,大概记录一下

~/.ssh/config

Host github.com *.github.com *.codeup.aliyun.com
    User git
    # SSH默认端口22,git://, HTTPS默认端口443,https://
    Port 22
    Hostname %h
    # 这里放你的SSH私钥
    IdentityFile ~\.ssh\id_rsa
    # 设置代理, 127.0.0.1:10808 换成你自己代理软件监听的本地地址
    # HTTPS使用-H,SOCKS使用-S
    ProxyCommand connect -S 127.0.0.1:10808 %h %p

需要用到connect客户端
connect
解压exe文件后放到C:\Windows\System32目录下面

linux下编译安装php8

下载:

https://www.php.net/downloads

configure

从宝塔安装的php看下通用的一些扩展编译参数
# php -i |grep configure

./configure 
--prefix=/www/server/php/74  --with-config-file-path=/www/server/php/74/etc  --enable-fpm  --with-fpm-user=www  --with-fpm-group=www  --enable-mysqlnd  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-iconv-dir  --with-freetype  --with-jpeg  --with-zlib  --with-libxml-dir=/usr  --enable-xml  --disable-rpath  --enable-bcmath  --enable-shmop  --enable-sysvsem  --enable-inline-optimization  --with-curl  --enable-mbregex  --enable-mbstring  --enable-intl  --enable-ftp  --enable-gd  --with-openssl  --with-mhash  --enable-pcntl  --enable-sockets  --with-xmlrpc  --enable-soap  --with-gettext  --disable-fileinfo  --enable-opcache  --with-sodium  --with-webp

稍微修改以下

--prefix=/usr/local/php/81 --with-config-file-path=/usr/local/php/81/etc  --enable-fpm  --with-fpm-user=www  --with-fpm-group=www  --enable-mysqlnd  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-iconv-dir  --with-freetype  --with-jpeg  --with-zlib  --with-libxml-dir=/usr  --enable-xml  --disable-rpath  --enable-bcmath  --enable-shmop  --enable-sysvsem  --enable-inline-optimization  --with-curl  --enable-mbregex  --enable-mbstring  --enable-intl  --enable-ftp  --enable-gd  --with-openssl  --with-mhash  --enable-pcntl  --enable-sockets  --with-xmlrpc  --enable-soap  --with-gettext  --disable-fileinfo  --enable-opcache  --with-sodium  --with-webp

configure跑跑看缺什么就装对应软件

缺少 安装
libxml-2.0 libxml2-dev
libcurl libcurl4-openssl-dev
libpng libpng-dev
libwebp libwebp-dev
libjpeg libjpeg-dev
freetype2 libfreetype-dev
oniguruma libonig-dev
libsodium libsodium-dev

….

安装

执行make && make install

RFID卡号格式的常见样式

北京友我科技RFID读写器 转载请注明出处,本篇地址
http://www.youwokeji.com.cn/ywdn/NoteDetails.asp?id=11

由于各个厂家的读卡器译码格式不尽相同,在读卡输出时,读出的二进制或十六进制(Hex)结果应该是唯一的,但是又可以通过以下几种主要换算办法,输出不同结果的十进制卡号(Dec),因此,请您一定在购买卡片或卡片喷号时,注意卡号格式的一致性:

1、格式0:10位十六进制的ASCII字符串,即10 Hex格式。
如:某样卡读出十六进制卡号为:“01026f6c3a”。

2、格式1:将格式0中的后8位,转换为10位十进制卡号,即8H—10D。
即将“ 026f6c3a”转换为:“0040856634”。

此格式喷码喷码较为常见。

3、格式2:将格式0中的后6位,转换为8位十进制卡号,即6H—8D。
即将“ 6f6c3a”转换为:“07302202”。

4、格式3:将格式0中的倒数第5、第6位,转换为3位十进制卡号,再将后4位,转换为5位十进制卡号,中间用“,”分开,即“2H + 4H”。
即将2H“ 6f”转换为:“111”,4H “6c3a”转为“27706”。 最终将2段号连在一起输出为“111,27706”。

此格式为标准的韦根26(V26)格式,只使用最后6位编码,也有许多卡采用此格式喷码。

5、格式4:将格式0中后8位的前4位,转换为5位十进制卡号,再将后4位,转换为5位十进制卡号,中间用“,”分开,即“4Hex + 4Hec”。
照此推算结果为:00623,27706 (4H+4H)