UUID
- 生成不重复的随机数
// uuid库 npm install uuid import {
v4 as uuidv4 } from 'uuid'; uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
讯享网
讯享网// nanoid库,比uuid库小一点 npm i nanoid import {
nanoid} from 'nanoid' conssole.log(nanoid())
/ * * @returns 生成不重复的随机序列号 '60a2cd4faec2474ca6ee43aac3b0bc1d' * 32位 16进制 */ export function getUUID() {
var d = new Date().getTime(); if (window.performance && typeof window.performance.now === "function") {
d += performance.now(); //use high-precision timer if available } var uuid = "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == "x" ? r : (r & 0x3) | 0x8).toString(16); }); return uuid; }
获取两个数之间的随机数
讯享网Math.random() *(max - min)+ min
从既定数组中随机选取
const area = [ '北京', '上海', '天津', '重庆', '河北', '山西', '吉林', '辽宁', '黑龙江', '陕西', '甘肃', '青海', '山东', '福建', '浙江', '河南', '湖北', '湖南', '江西', '江苏', '安徽', '广东', '海南', '四川', '贵州', '云南' ]; const lastName = [ '赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻' ]; let userlist = []; let date = new Date(); const i = Math.floor(Math.random() * 10); for (let index = 0; index < 20; index++) {
let day = date; if (index > i) day = new Date(Date.now() - ); userlist.push({
area: area[Math.floor(area.length * Math.random())], //取area中的任意一项 last: lastName[Math.floor(lastName.length * Math.random())], //取lastName中的任意一项 gender: Math.random() > 0.7 ? '女士' : '先生', //二选一 time: `${
day.getMonth() + 1}月${
day.getDate()}日` //前i条是今天,后(20-i)条是昨天 }); } list = userlist;
小程序随机电话号
讯享网 data: {
swiper: [ {
tel: `1${
Math.floor(Math.random() * 7 + 3)}${
Math.floor( Math.random() * 10 )}${
Math.floor(Math.random() * 9000 + 1000)}`, price: Math.floor(Math.random() * 900 + 100), }, {
tel: `1${
Math.floor(Math.random() * 7 + 3)}${
Math.floor( Math.random() * 10 )}${
Math.floor(Math.random() * 9000 + 1000)}`, price: Math.floor(Math.random() * 900 + 100), }, ], }, methods: {
/ * 生命周期函数--监听页面加载 */ async onLoad() {
let arr = [] for (let i = 0; i < 20; i++) {
arr.push({
tel: `1${
Math.floor(Math.random() * 7 + 3)}${
Math.floor( Math.random() * 10 )}${
Math.floor(Math.random() * 9000 + 1000)}`, price: Math.floor(Math.random() * 900 + 100), }) } this.setData({
swiper: arr, }) },
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/100214.html