Solved

Random number not so random?

Posted on 2003-10-31
8
1,095 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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