Next: , Previous: Selecting by Position, Up: Querying Recfiles

3.4 Random Records

Consider a database in which each record is a cooking recipe. It is always difficult to decide what to cook each day, so it would be nice if we could ask recsel to pick up a random recipe. This can be achieved using the -m (--random) command line option of recsel:

     $ recsel -m 1 recipes.rec
     Title: Curry chicken
     Ingredient: A whole chicken
     Ingredient: Curry
     Preparation: ...

If we need two recipes, because we will be cooking at both lunch and dinner, we can pass a different number to -m:

     $ recsel -m 2 recipes.rec
     Title: Fabada Asturiana
     Ingredient: 300 gr of fabes.
     Ingredient: Chorizo
     Ingredient: Morcilla
     Preparation: ...
     Title: Pasta with ragu
     Ingredient: 500 gr of spaghetti.
     Ingredient: 2 tomatoes.
     Ingredient: Minced meat.
     Preparation: ...

The algorithm used to implement -m guarantees that you will never get multiple instances of the same record. This means that if a record set has n records and you ask for n random records, you will get all the records in a random order.