Solved

Random number not so random?

Posted on 2003-10-31
8
1,067 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now