Alpha Video Training
Results 1 to 3 of 3

Thread: Random record selection

  1. #1
    Member MikeData's Avatar
    Real Name
    Mike Data
    Join Date
    Nov 2008
    Posts
    923

    Default Random record selection

    I am trying to extract a random record from the quotes table.
    See attached sample

    I am doing it from the mainmenu form.
    I set up a set of steps in the calculated variables to do it.

    VquoteNo = rand()*100
    vid = int(calc->VquoteNo)/10
    VidChar = "00"+alltrim(Str(calc->vid))
    vquote = LOOKUP("quotes.dbf","id ="+QUOTE(calc->VidChar),"qte")


    Questions:
    Is there a more direct or more efficient way to apply the random extraction from the quotes table to the variable vquote?

    The problem is the issue of the "00", it will be a limitation.

  2. #2
    Member
    Real Name
    Lee Goldberg
    Join Date
    Jun 2008
    Location
    Wilmington, DE
    Posts
    206

    Default Re: Random record selection

    I created a random number generator function a couple weeks ago. You could simply make the functions parameters equal to the following and then get a random record number in return and then use that number to bring up the record on a form...

    So Import the function below that I created which allows you to generate a true random number using the random.org API. Be Sure to name the Function "RandNums"
    1.)Goto the control panel and select the "code" tab
    2.)Press the "New" button at the top of the control panel window
    3.)Select the "Function" from the drop down list
    4.)Enter the value "RandNums" in the Function Name field on the window that pops us
    5.)Press "Ok" on the new function window (just Leave the function Return Type as "Character")
    6.)Now a new window will popup in the code editor with the outline for a new function. Erase everything in that window and paste all of the code below
    7.) Hit the save button to save the new function you entered

    Code:
    'Date Created: 25-Jan-2011 04:28:47 PM
    'Last Updated: 08-Feb-2011 03:34:37 PM
    'Created By  : Lee Goldberg - Shore Consultants Ltd
    'Updated By  : Lee Goldberg - Shore Consultants Ltd
    
    FUNCTION RandNums AS C (vNumOfNums AS N,vMyMin AS N,vMyMax AS N)
    	DIM vp as p
    	DIm vurl as c
    	DIM vbody as c
    	DIM vresponse as c
    	DIM vReturn as c
    	vurl = "http://www.random.org/integers/?num="+vNumOfNums-"&min="+vMyMin-"&max="+vMyMax-"&col=1&base=10&format=plain&rnd=new"
    	vbody = "" 
    	vp = http_post(vurl,vbody,"",80,2800,.f.)
    '	msgbox("vp",vp.body)
    	
    	vresponse = *HTML_TO_PLAIN(vp.body)
    	
    	if left(vresponse,10) = "RANDOM.ORG" then
    	    msgbox("Error!","Parameters do not meet criteria")
    	    vReturn = "Error! Please try again"
    	else
    		vReturn = alltrim(vresponse)
    	end if
    	
    	RandNums = vReturn
    END FUNCTION

    Now, add a new xBasic Script (Using xBasic Editor) on the "code" tab in the control panel (Select the "New" button->then select "Script(Using xBasic Editor)" )and add copy and paste the code in the box below to your new xBasic Script. You can Save this code with whatever name you want. Use This name to referenced the code on the OnClick action of a button placed on a form you want to preform this action. You can also use it however you would like.

    Code:
    'Date Created: 08-Feb-2011 03:34:59 PM
    'Last Updated: 08-Feb-2011 03:34:59 PM
    'Created By  : Lee Goldberg - Shore Consultants Ltd
    'Updated By  : Lee Goldberg - Shore Consultants Ltd
    DIM pFrmMyFrm as p 'Pointer to the form you want to open to show the random record 
    DIM vRandRecNo as N  'The Value the function will return
    DIM vRandNums as N 'How many random numbers you want the function to return.  In this case you just want one number
    DIM vMinimum as N 'The minimum number the random generator can select from
    DIM vMaximum as N 'The maximum number the random generator can select from
    DIM vMyTblName as c 'The Name of the table you want a random record for
    DIM vMyFrmName as c 'The Name of the form you want to open to show the random record
    
    vMyTblName = "tbllotto" 'change the name to the table you want to select the random record from
    vMyFrmName = "lotto" 'change this to the name of the form you want to show the random record you will generate below
    vRandNums = 1 'is "1" because you only want one record number to be returned. (function can return more than one)
    vMinimum = 1 'you can make this higher depending on if you only want the random number to be selected from a certain range of records
    vMaximum = tablecount(alltrim(vMyTblName),".t.")
    
    'Stores the random number returned by the RandNums function in the variable vRandRecNo for later use
    'RandNums can return a string of random numbers (but this case just one number)
    'So RandNums function will return a single integer value that is 
    'greater than or equal to vMinimum and less than or equal to vMaximum (as defined above)
    
    vRandRecNo = val(RandNums(vRandNums,vMinimum,vMaximum)) 'Returns a random Number from the RandNums Function and uses the Val() function to make the char string it returns numeric type
    
    'Show the random selected record number to the user
    msgbox("Random Record #","Random Record Number: "+crlf(2)+"    "+alltrim(str(vRandRecNo))+" of the " + alltrim(str(vMaximum))+" totals records in the table "+alltrim(vMyTblName))
    
    'Here you can open a form that shows the records from the table you selected
    
    'First load (open) the form and reference it to the variable fMyFrm for later manipulation
    'Then clear the form of any current queries that may prohibit the from from showing the random record
    'then tell the form to Goto the random record number
    'then repaint (or refresh) the form so that it shows the user the random record number we just told it to goto.
    
    pFrmMyFrm = form.load(alltrim(vMyFrmName))
    pFrmMyFrm.Query_Clear()
    pFrmMyFrm.Recno_goto(vRandRecNo)
    pFrmMyFrm.Repaint()
    
    'End of Code
    'let me know if you have any questions
    'lee@shorecon.com
    Hope this helps
    Last edited by goldberg2727; 02-08-2011 at 05:13 PM.
    Lee Goldberg
    Software Development and Sales
    Shore Consultants Ltd.
    lee@shorecon.com
    www.shorecon.com

  3. #3
    Member MikeData's Avatar
    Real Name
    Mike Data
    Join Date
    Nov 2008
    Posts
    923

    Default Re: Random record selection

    Thanks for the response. I will try it.
    I did respond to this was because, I did not get an email that the thread was answered.

Similar Threads

  1. Selecting a random record
    By baldguy in forum Alpha Five Version 10 - Desktop Applications
    Replies: 7
    Last Post: 01-25-2011, 05:41 PM
  2. Random Record Marking
    By programsplus in forum Alpha Five Version 7
    Replies: 7
    Last Post: 06-15-2007, 09:45 AM
  3. Random truncation and case change in Record List b
    By Bob Pickle in forum Alpha Five Version 5
    Replies: 2
    Last Post: 12-22-2003, 12:33 PM
  4. Random Select Record?
    By Leslie Blades in forum Alpha Five Version 5
    Replies: 5
    Last Post: 07-07-2003, 05:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •