Skip to main content
Participant
December 5, 2008
Question

Random numbers

  • December 5, 2008
  • 2 replies
  • 501 views
I have incorporated some javascript into a web app im working on. The javascript function generates a random number between (l)ow and (h)igh. I'd prefer to do this in coldfusion if its possible. Here is the jscript code:

// generate a random number between l and h
function gen_random_number(l,h)
{
return Math.floor(Math.random()*h)+l;
}

Is there a colfusion equivalent ?
This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
December 14, 2008
DioPally,
I suspect an error in your fomula. I have

Javascript
=========
<script type="text/javascript">
// generate a random number between L and H
function gen_random_number(L,H)
{
return L + Math.floor(Math.random()*(H - L + 1));
}
</script>

Coldfusion
=========
<cfscript>
// generate a random number between L and H
function gen_random_number(L,H)
{
return L + int(Rand("SHA1PRNG")*( H - L + 1));
}
</cfscript>

This is a literal translation to show the corresponding Coldfusion functions. Dan Bracuk's randRange is the better solution.

Inspiring
December 5, 2008
randrange()