Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

showing random records from a database


  • From: cj@xxxxxxxxxxxxxx (Cynthia Jeness)
  • Subject: showing random records from a database
  • Date: Wed, 29 Nov 2000 11:42:50 -0500

Marcos,

This code will generate a set of random numbers:
  /**
    * This method selects "count" numbers at random between
    * the "low" and "high", inclusive.
    *
    * @param   low   Lowest number to select.
    * @param   high  Highest number to select.
    * @param   count How many numbers to select.
    *
    * @return        Array of the integer numbers selected.
    */
   public static  int [] pickNumbers(int low, int high, int count) {

      Random random = new Random();
      int range = high - low + 1;

      int [] num = new int[range];
      for (int i=0, start=low; i < range; i++, start++)
         num[i] = start;

      for (int i=0; i < count; i++) {
         int which = i+random.nextInt(range-i);
         int tmp = num[i];
         num[i] = num[which];
         num[which] = tmp;
      }

      int [] num1 = new int[count];
      System.arraycopy(num, 0, num1, 0, count);
      return num1;
   }

Marcos wrote:

> hi,
>
>     i have a database with 100 records. nowadays i have some servlets to
> show the data from the database. but i would like to pick up, randomly,
> a number of records.
>     i mean something like i make a query which puts all the records to
> the resultset. i make an array with 6 numbers randomly generated. so i
> will be moving across the resultset and when i get the position which
> number i got randomly i will show it.
>
> any suggestions will be appreciated,
>
> thanks in advance,
>
> marcos
>
> --
> m a r c o s @ i v a l . e s
>
> ---
> To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
> To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx


---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx