Solved

Random number not so random?

Posted on 2003-10-31
8
1,103 Views
Last Modified: 2013-12-24
Sometimes the simplest things seem so hard!!

Okay, here's what I've got...

<CFSET r=randomize(1000)>
<CFSET r=RandRange(1,10000)>

<cfif r lt 5000>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
Feature 1
<cfelse>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
Feature 2
</cfif>

Easy, right? Well... every time the page loads I get the same "random" number for "r". 5749 to be exact.

Grrr.

WHY!?
0
Comment
Question by:alyoung
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Expert Comment

by:CFDevHead
ID: 9659689
Try this instead
<CFSET r=rand()>
<cfoutput>#r#</cfoutput>
<CFSET r=RandRange(1,10000)>
<cfoutput>#r#</cfoutput>
<cfif r lt 5000>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
Feature 1
<cfelse>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
Feature 2
</cfif>
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9659775
randomize is seeding the random # generator. If you seed the random gen with the same # the same number wil be returned. What you will want to do is instead of using a fixed number use a number that changes. Like now().
<CFSET r=randomize(now())>
<CFSET r=RandRange(1,10000)>

<cfif r lt 5000>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
Feature 1
<cfelse>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
Feature 2
</cfif>

Or just don't seed it :)
0
 

Author Comment

by:alyoung
ID: 9660130
Thanks PE_CF_DEV; I understand why that would work better.

Is there some server-side cache issue that might be at work here? I mean, I have this code that never changes upon reload. The same information is generated when I view it from another browser as well! What's with that?!

<CFSET r=randomize(now())>
<cfset i = 1>
<!--- just testing randomness --->
<cfloop condition="i lt 11">
      The random number is: <cfoutput>#rand()#</cfoutput><BR>
      <cfset i = i + 1>
</cfloop>
<CFSET r=RandRange(1,10000)>
<cfif r lt 5000>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
Featured item:
<cfelse>
<p> --> <cfoutput>#r#</cfoutput> <-- </p>
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 17

Expert Comment

by:anandkp
ID: 9661976
y r u using randomize ???

just use randrange(1,1000) & u shld be thru with it !

<cfif randrange(1,1000) lt 5000>
Feature 1
<cfelse>
Feature 2
</cfif>

let me know !

K'Rgds
Anand
0
 

Author Comment

by:alyoung
ID: 9665865
Why? To quote from Macromedia's CFMX 6.1 CFML Reference:

"Randomize
[...]
Usage
Call this function before calling Rand. Although this function returns a decimal number, it is not
a random number."

Regardless, when I'm using just randrange, I still get a random number on the first load, and then the same number on reloads in that browser and across different browsers.
0
 
LVL 17

Expert Comment

by:anandkp
ID: 9668667
yes - randomise is used in conjunction with rand.
but i was just using randrange ...

it worked perfect for me !
I dont see how it can give same number everytime !

really weird !
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9670999
I am not sure how randmize works...however I know it does not give a different random number. As an example in macromedias documentation it give an example of using 5 and it in the book it saysi it returns .57079106 and sure enough when I ran it it returned the same result everytime. So like I said it seeds something to give ytou a  random number but that random number is always the same for each input. I am not sure what the intented use of it is though? Maybe it is ideally used for a low level of encyption??? Sort of like hashing (only not as secure). An example would be someone gives you a password. You then would updated the password as password=#randomize(password)# that would stored the randomized version in the database then on login you would test for query.password EQ  randomize(login.password). Other than that I have no idea what that function is usefull for....as has been stated take out the randomize function and it all works fine. I'll send something to Forta about it and see what his response is :) Ussually take a week or 2 for him to respond though.
0
 
LVL 9

Accepted Solution

by:
CFDevHead earned 125 total points
ID: 9671668
After reading the definitions below you will see that randrange is the function that you will want to use to generate a random whole number.

Randomize
Seeds the ColdFusion random number generator with an integer number. Seeding the generator helps ensure that the Rand function generates highly random numbers.

A non-random decimal number, in the range 0 – 1.

Mathematical functions

Randomize(number)
Rand, RandRange

Parameter
 Description
 
number
 A number
 

Call this function before calling Rand. Although this function returns a decimal number, it is not a random number.

<h3>Randomize Example</h3>
<p>Call Randomize to seed the random number generator. This helps to ensure the randomness of numbers generated by Rand.
<cfif IsDefined("FORM.myRandomInt")>
      <cfif IsNumeric(FORM.myRandomInt)>
        <cfoutput><p><b>Seed value is #FORM.myRandomInt#</b>
         </cfoutput><br>
    <cfset r = Randomize(FORM.myRandomInt)>
        <cfloop index = "i" from = "1" to = "10" step = "1">
              <cfoutput>Next random number is #Rand()#</cfoutput><br>
        </cfloop><br>
      <cfelse>
            <p>Please enter a number.
      </cfif>
</cfif>
<form action = "randomize.cfm">
<p>Enter a number to seed the randomizer:
<input type = "Text" name = "MyRandomInt">
<p><input type = "Submit" name = "">
</form>


Generates a random number.

A random decimal number, in the range 0 – 1.

Mathematical functions

Rand()
Randomize, RandRange

To ensure greater randomness, call the Randomize function before calling Rand.

<h3>Rand Example</h3>
<p>Rand() returns a random number in the range 0 to 1.
<p>Rand() returned: <cfoutput>#Rand()#</cfoutput>
<p><A HREF = "rand.cfm">Try again</A>


RandRange
Generates a random integer between two specified numbers. Requests for random integers that are greater than 100,000,000 result in non-random numbers, to prevent overflow during internal computations.

A random integer

Mathematical functions

RandRange(number1, number2)
Rand, Randomize

Parameter
 Description
 
number1, number2
 Integer numbers less than 100,000,000
 

<h3>RandRange Example</h3>
<p>RandRange returns an integer between two specified integers.
<cfif IsDefined("FORM.myInt")>
      <p>RandRange returned:
            <cfoutput>#RandRange(FORM.myInt, FORM.myInt2)#</cfoutput>
</cfif>

<cfform action = "randRange.cfm">
<p>Enter a number to seed the randomizer:
<cfinput type = "Text" name = "MyInt" value = "1" RANGE = "1,100000000"
 message = "Please enter a value between 1 and 100,000,000"
  validate = "integer" required = "Yes">
<cfinput type = "Text" name = "MyInt2" value = "500" RANGE = "1,100000000"
 message = "Please enter a value between 1 and 100,000,000"
  validate = "integer" required = "Yes">
<p><input type = "Submit" name = "">
</cfform>
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question