Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Random number not so random?

Posted on 2003-10-31
8
Medium Priority
?
1,155 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview

885 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