Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a random number in JSTL?

Tags:

jsp

jstl

el

I would like to get something like the next code generated in JSTL

<c:choose>
    <c:when test="${random number is even}">
        <div class="redlogo">
    </c:when>
    <c:otherwise>
        <div class="greenlogo">
    </c:otherwise>
</c:choose>
like image 592
Sergio del Amo Avatar asked Feb 16 '10 13:02

Sergio del Amo


People also ask

How do I generate a random number in Swiftui?

To generate a random number in Swift, use Int. random() function. Int. random() returns a number, that is randomly selected, in the given range.

What is Math random?

Math.random() The Math.random() function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range.


1 Answers

This one is a bit ugly but it works...

<c:set var="rand"><%= java.lang.Math.round(java.lang.Math.random() * 2) %></c:set>

Later you can check for ${rand mod 2 == 0} and ${rand mod 2 == 1} to get your desired output.

like image 105
Ricardo Marimon Avatar answered Sep 23 '22 06:09

Ricardo Marimon