English 中文(简体)
如何在自由标识模板中随机分类?
原标题:How to get a random integer in Freemarker template?
问题回答

Freemarker does not provide a random number generator at the moment. You can implement a naive random number generator using the time ( .now ) as a seed, but it should never be a critical part of your program.

你可以在 Java产生随机的愤怒,并将这种愤怒传递给自由标识模板。

另一种选择是创建自己的Freemarkermeth,并将其注入数据模型。

这方面的一个例子是 Java8如何做到这一点:

public String generate(Map<String, Object> data, String templateLocation) throws IOException, TemplateException {
    try (StringWriter writer = new StringWriter()) {
        Template template = configuration.getTemplate(templateLocation);
        data.put("uuid", (TemplateMethodModelEx) (list) -> UUID.randomUUID());
        Random r = new Random();
        // Adding method for generation of random number
        data.put("randomNumber", (TemplateMethodModelEx) (list) -> r.nextInt(200));
        template.process(data, writer);
        return writer.toString();
    }
}

在“自由标识”模板中,你将能够使用这种方法:

${randomNumber()}

我只是把一个相对随机的生成器放在一起。 采用<代码>.now?long的微秒,并用你的种子(salt)加以多倍。 (就第7条而言)和三者之间的差距。 在我的案件中,我只需要5个数字,但每天可以达到每秒13位数。 或者说,如果你想增加更多的乘数、增加数等等的话。

<#function randomNumber salt>
    <#local str= .now?long />
    <#assign str = (str * salt)/3 />
    <#return str[(str?string?length-5)..]/>
</#function>


<#assign rnd = randomNumber(7)/>

如果你不想要salt,你就只能删除计算中的参数和硬代码salt变量和仅打电话randomNumber(

Let s say you want to add a random image out of some set to each listed item. So get a seed, and then rotate that seed in some way.

<#assign photos>
  DSC_0005.jpg
  DSC_0008.jpg
  DSC_0013.jpg
  DSC_0020.jpg
</#assign>
<#assign photos = photos?split( \n ,  rmc ) >
<#assign nextRandom = .now?string["HHmmssSSS"]?number>

<#list posts as post>
     <div class="banner divider" style="background-image: url(photos/panoramas/${photos[nextRandom % photos?size]});"></div>
     <#assign nextRandom = nextRandom * 13 % 104729>
</#list>

我选择了<代码>nextRandom = 下一个Random * 13 % 104729,但我必须从统计角度讲,这非常不是原则。 还确保104729人比你收集的钱多。

。 专用变量参考资料


更好的办法,至少对于超文本处理,是把这种收集工作带往 Java,并从那里收集:

<script type="text/javascript">
    var photos = [ <#list photos as photo>  ${photo} , </#list> ];
    var randomPhoto = photos[Math.floor(photos.length * Math.random())];
</script>

See e.g. Math.random( at MDN.





相关问题
Weighted random numbers

I m trying to implement a weighted random numbers. I m currently just banging my head against the wall and cannot figure this out. In my project (Hold em hand-ranges, subjective all-in equity ...

Comprehensive information about hash salts

There are a lot of questions about salts and best practices, however most of them simply answer very specific questions about them. I have several questions which feed into one another. Assuming a ...

Generate unique names?

I am working on a php site in which we have to upload images from users.i have to rename that file for preventing conflicts in the name of the image. uniqid(rand(), true); and adding a large random ...

How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

热门标签