Solved

Random number not so random?

Posted on 2003-10-31
8
1,131 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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