Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Generators(random only once)

Status
Not open for further replies.

jl8789

MIS
May 22, 2003
293
US
I am trying to use various methods of generating a 6 digit random number. They all generate a random number the first time the page is loaded. Every call to it after that produces the exact same numbers. I am trying to reevaluate the page etc on every call to it but that doesn't work. How do I get random numbers every time?

<html>
<head>
<title>Untitled Document</title>
<META http-EQUIV="Expires" CONTENT="Mon, 01 Jan 1970 23:59:59 GMT">
<CFHEADER NAME="Pragma" VALUE="no-cache">
<CFHEADER NAME="Expires" VALUE="Now()">
<CFHEADER NAME="cache-control" VALUE="no-cache, no-store, must-revalidate">

</head>

<body>
<cfset r1 = Randomize(now())>
<cfset r2 = Randomize(now())>
<cfset r3 = Randomize(now())>
<cfset r4 = Randomize(now())>
<cfset r5 = Randomize(now())>
<cfset r6 = Randomize(now())>

<p>RandRange returned:
<cfset r1 = #RandRange(1, 9)#>
<cfset r2 = #RandRange(1, 9)#>
<cfset r3 = #RandRange(1, 9)#>
<cfset r4 = #RandRange(1, 9)#>
<cfset r5 = #RandRange(1, 9)#>
<cfset r6 = #RandRange(1, 9)#>
<cfoutput>#r1#</cfoutput>
<cfoutput>#r2#</cfoutput>
<cfoutput>#r3#</cfoutput>
<cfoutput>#r4#</cfoutput>
<cfoutput>#r5#</cfoutput>
<cfoutput>#r6#</cfoutput>


<cfloop index="Myindex" from="1"
to="6" step="1">
<cfloop index="randomindex"
from="1" to="9" step="1">
<cfset random=rand()>
</cfloop>
<cfoutput>
Magic number number #Myindex#
is:&nbsp;&nbsp;#RandRange
(1,9)#<br><br>
</cfoutput>
</cfloop>



<cfloop index="Myindex" from="1"
to="1" step="1">
<cfloop index="randomindex"
from="1" to="100000" step="1">
<cfset random=rand()>
</cfloop>
<cfoutput>
number number #Myindex#
is:&nbsp;&nbsp;#RandRange
(100000,999999)#<br><br>
</cfoutput>
</cfloop>


LAST EXAMPLE
<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(100000,999999)>
<cfoutput>#r#</cfoutput>


GENERATOR
<cfscript>
//Initialize variables
variables.itemCount = 1;
variables.bannerlist = '';
variables.retrieveIDList = '';
variables.retrieved = 6;// how many random numbers do you want to select

// Set the Min and Max Variables for the Banners
variables.begin = 1; // begin range
variables.end = 9; // end range
//Loop until variables.retrieved unique numbers have been randomly chosen
while( Variables.ItemCount LTE Variables.Retrieved ){
//Pick a random ID number based on the minimum and maximum ID
Variables.RandomID = RandRange(Variables.begin, Variables.end);
//If the new ID is not already in the results list, add it
if( ListFind(Variables.RetrieveIDList, Variables.RandomID) EQ 0 ){
//A random ID has been found that is in the list and is not in the results list...increment the counter
Variables.ItemCount = Variables.ItemCount + 1;
Variables.RetrieveIDList = listAppend(Variables.RetrieveIDList, Variables.RandomID);
}
}

</cfscript>
<cfoutput>=== #Variables.RetrieveIDList# ===</cfoutput>


RANDOM 6 digit CHARACTER PASSWORD
<cfset characterList = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z">

<!--- create the random password --->
<CFSET pwd="">
<CFLOOP FROM="1" TO="6" INDEX="i">
<CFSET randNum = RandRange(1,ListLen(characterList))>
<CFSET randLetter = ListGetAt(characterList, #randNum#)>
<CFSET pwd = pwd & randLetter>
</CFLOOP>

<cfoutput>#pwd#</cfoutput>

</body>
</html>
 
TAKE OUT THE
= Randomize(now())
AND MAKE IT
= 0>

------------------------------------------------------------------------------
brannonH
if( !succeed ) try( );
 
Here is the code........

Code:
<html>
<head>
<title>Untitled Document</title>
<META http-EQUIV="Expires" CONTENT="Mon, 01 Jan 1970 23:59:59 GMT">
<CFHEADER NAME="Pragma" VALUE="no-cache">
<CFHEADER NAME="Expires" VALUE="Now()">
<CFHEADER NAME="cache-control" VALUE="no-cache, no-store, must-revalidate">

</head>

<body>
<cfscript>
	r1 = RandRange(1, 9);
	r2 = RandRange(1, 9);
	r3 = RandRange(1, 9);
	r4 = RandRange(1, 9);
	r5 = RandRange(1, 9);
	r6 = RandRange(1, 9);
</cfscript>

<p>RandRange returned:
<cfoutput>#r1#</cfoutput>
<cfoutput>#r2#</cfoutput>
<cfoutput>#r3#</cfoutput>
<cfoutput>#r4#</cfoutput>
<cfoutput>#r5#</cfoutput>
<cfoutput>#r6#</cfoutput>
<br>
<cfoutput>
<cfloop index="Myindex" from="1" to="6">
	<cfloop index="randomindex" from="1" to="9">
		<cfset random=rand()>
	</cfloop>
	<br>
	Magic number number #Myindex# is:&nbsp;&nbsp;#RandRange (1,9)#<br>
</cfloop>
</cfoutput>

<cfoutput>
<cfloop index="Myindex" from="1" to="1">
   <cfloop index="randomindex" from="1" to="100000">
	   <cfset random=rand()>
   </cfloop>
  <br><br>number number #Myindex# is:&nbsp;&nbsp;#RandRange (100000,999999)#<br>
</cfloop>
</cfoutput>
<br>
<br>
LAST EXAMPLE
<cfset r = 0>
<cfset i = 1> 
<!--- just testing randomness ---> 
<cfloop condition="i lt 11">
	<br>The random number is: <cfoutput>#rand()#</cfoutput>
	<cfset i = i + 1>
</cfloop>
<CFSET r = RandRange(100000,999999)>
<cfoutput>#r#</cfoutput>
<br><br>
GENERATOR
<cfscript>
    //Initialize variables
    variables.itemCount = 1;
    variables.bannerlist = '';
    variables.retrieveIDList = '';
    variables.retrieved = 6;// how many random numbers do you want to select
    
    // Set the Min and Max Variables for the Banners
    variables.begin = 1;         // begin range
    variables.end = 9;     // end range
    //Loop until variables.retrieved unique numbers have been randomly chosen
    while( Variables.ItemCount LTE Variables.Retrieved ){
        //Pick a random ID number based on the minimum and maximum ID
        Variables.RandomID = RandRange(Variables.begin, Variables.end);
        //If the new ID is not already in the results list, add it
        if( ListFind(Variables.RetrieveIDList, Variables.RandomID) EQ 0 ){
            //A random ID has been found that is in the list and is not in the results list...increment the counter
            Variables.ItemCount = Variables.ItemCount + 1;
            Variables.RetrieveIDList = listAppend(Variables.RetrieveIDList, Variables.RandomID);
        }
    }
    
</cfscript>
<cfoutput>=== #Variables.RetrieveIDList# ===</cfoutput> 

<br>
<br>
RANDOM 6 digit CHARACTER PASSWORD = 
<cfset characterList = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z">
<!--- create the random password --->
<CFSET pwd="">
<CFLOOP FROM="1" TO="6" INDEX="i">
  <CFSET randNum = RandRange(1,ListLen(characterList))>
  <CFSET randLetter = ListGetAt(characterList, randNum)>
  <CFSET pwd = pwd & randLetter>
</CFLOOP>
<cfoutput><strong>#pwd#</strong></cfoutput>
</body>
</html>

------------------------------------------------------------------------------
brannonH
if( !succeed ) try( );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top