生成不同的聪明随机数

编程入门 行业动态 更新时间:2024-10-27 16:38:35
本文介绍了生成不同的聪明随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是smarty v2.6,我想生成随机的不同数字.我正在寻找一种高效,快速的方法来使用已经提供的Smarty函数.这是我的代码,用于生成5个随机数(但不是唯一的):

I'm using smarty v2.6 and I want to generate random distinct numbers. I'm looking for an efficient, fast way to do it using already provided Smarty functions. This is my code for generating 5 random numbers (but not distinct):

{assign var=min value=1}{assign var=max value =5} {section name=val start=$min loop=$max+1} {assign var=random value=1|mt_rand:15} {$random} {/section}

推荐答案

如果您真的需要在智能模板中完成

if you really need to do it in smarty templates

方法1

{assign var="distinct_numbers" value=array_fill(1,15,'x')} {assign var="distinct_numbers" value=array_keys($distinct_numbers)} {assign var="x" value=shuffle($distinct_numbers)} {* result *} {foreach from=$distinct_numbers item="value"} {$value} | {/foreach} 1 | 7 | 3 | 10 | 4 | 8 | 6 | 14 | 13 | 12 | 2 | 5 | 11 | 9 | 15 |

方法2

  • array_fill()
  • array_keys()
  • array_rand()+ unset()代替shuffle()

更多推荐

生成不同的聪明随机数

本文发布于:2023-10-28 23:05:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   聪明

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!