Select 4 records, Randomly from Database

Discussion in 'ASP General' started by tamaker, Dec 12, 2007.

  1. tamaker

    tamaker Guest

    Is this do-able with ASP / VBscript?

    -- I have a database with user records (name, photo, etc. etc.)

    I want to use asp to generate (on the homepage) a series of 4 randomly
    selected 'user records' from the database -- say just the headshot
    photo or name from the database.

    In addition to the recordset being randomly generated (i.e. our of
    about 50 records, only records 4, 18, 23 and 26 are displayed) I need
    to MAKE SURE they are not duplicated (i.e. out of the 50 records,
    records 4, 18, 23 and 23 -- where the last two are duplicates).

    Is there a way I can pull this off and how do I go about it exactly?


    Thanks!
    tamaker, Dec 12, 2007
    #1
    1. Advertising

  2. tamaker

    Bruce Guest

    1) get the number of records (users in the database)
    2) generate 4 random numbers, make sure that they are not the same, and the
    they are not higher than the total number of users (let's say that you got
    12,25,28 and 30)
    3) choose the highest number that you selecte (among the 4 random numbers)
    (that will be 30)
    4) select top 30 * from users (assuming that 30 is the highest number). This
    will get 30 users from the database
    5) loop thru the records, when you're at the random numbers, display them
    (meaning that you display only 12,25,28 and 30).

    This isn't the most optimized way, but try it and make it work. You can
    always tweak it later.

    Hope this helps
    B

    "tamaker" <> wrote in message
    news:...
    > Is this do-able with ASP / VBscript?
    >
    > -- I have a database with user records (name, photo, etc. etc.)
    >
    > I want to use asp to generate (on the homepage) a series of 4 randomly
    > selected 'user records' from the database -- say just the headshot
    > photo or name from the database.
    >
    > In addition to the recordset being randomly generated (i.e. our of
    > about 50 records, only records 4, 18, 23 and 26 are displayed) I need
    > to MAKE SURE they are not duplicated (i.e. out of the 50 records,
    > records 4, 18, 23 and 23 -- where the last two are duplicates).
    >
    > Is there a way I can pull this off and how do I go about it exactly?
    >
    >
    > Thanks!
    >
    Bruce, Dec 12, 2007
    #2
    1. Advertising

  3. tamaker

    Bob Lehmann Guest

    http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.html

    Bob Lehmann

    "tamaker" <> wrote in message
    news:...
    > Is this do-able with ASP / VBscript?
    >
    > -- I have a database with user records (name, photo, etc. etc.)
    >
    > I want to use asp to generate (on the homepage) a series of 4 randomly
    > selected 'user records' from the database -- say just the headshot
    > photo or name from the database.
    >
    > In addition to the recordset being randomly generated (i.e. our of
    > about 50 records, only records 4, 18, 23 and 26 are displayed) I need
    > to MAKE SURE they are not duplicated (i.e. out of the 50 records,
    > records 4, 18, 23 and 23 -- where the last two are duplicates).
    >
    > Is there a way I can pull this off and how do I go about it exactly?
    >
    >
    > Thanks!
    >
    Bob Lehmann, Dec 13, 2007
    #3
  4. tamaker

    Dooza Guest

    tamaker wrote:
    > Is this do-able with ASP / VBscript?
    >
    > -- I have a database with user records (name, photo, etc. etc.)
    >
    > I want to use asp to generate (on the homepage) a series of 4 randomly
    > selected 'user records' from the database -- say just the headshot
    > photo or name from the database.
    >
    > In addition to the recordset being randomly generated (i.e. our of
    > about 50 records, only records 4, 18, 23 and 26 are displayed) I need
    > to MAKE SURE they are not duplicated (i.e. out of the 50 records,
    > records 4, 18, 23 and 23 -- where the last two are duplicates).
    >
    > Is there a way I can pull this off and how do I go about it exactly?


    I have a stored procedure that does this, here is the SQL is uses:

    SELECT TOP 5 item, desc, picture
    FROM dbo.products
    ORDER BY NEWID()

    I haven't seen it create any duplicates yet.

    Steve
    Dooza, Dec 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Airig
    Replies:
    0
    Views:
    788
    Luke Airig
    Dec 31, 2003
  2. Davy

    Select element from Set randomly?

    Davy, Aug 22, 2005, in forum: C Programming
    Replies:
    4
    Views:
    368
    Keith Thompson
    Aug 22, 2005
  3. Robert
    Replies:
    6
    Views:
    469
    Robert
    Aug 23, 2005
  4. haiwen

    can we randomly select mutiple days in calendar?

    haiwen, Oct 2, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    110
    Yan-Hong Huang[MSFT]
    Oct 6, 2003
  5. Scott Gordo

    ASP/VBScript to randomly select name from csv?

    Scott Gordo, Mar 9, 2006, in forum: ASP General
    Replies:
    2
    Views:
    168
    MikeR
    Mar 10, 2006
Loading...

Share This Page