生成不同的聪明随机数 [英] Generate distinct smarty random numbers

查看:56
本文介绍了生成不同的聪明随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是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()

这篇关于生成不同的聪明随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆