diff --git a/relation.module b/relation.module
index d311160..e04064d 100644
--- a/relation.module
+++ b/relation.module
@@ -630,3 +630,66 @@ function relation_views_api() {
     'path' => drupal_get_path('module', 'relation') . '/views',
   );
 }
+
+/**
+ * Generates random relations.
+ 
+1. loop over each relation type (assume relation types are already created by the user).
+2. For each relation type, get the available endpoint types (checking if the relation type is directional)
+3. randomly choose an endpoint type from those available, then do an EFQ with entityOrderBy('RAND()') (if that works)
+4. Repeat an n-1 times, where min_arity<=n<=max_arity
+5. create the relations.
+ */
+function relation_generate_relations($number = 10, $types = array(), $kill = FALSE) {
+  if ($kill) {
+    $query = new EntityFieldQuery();
+    $results = $query->entityCondition('entity_type', 'relation')
+      ->execute();
+    $rids = array_keys($results['relation']);
+    relation_multiple_delete($rids);
+  }
+  $relation_types = relation_get_types($types);
+  $rids = array();
+  foreach ($relation_types as $type => $relation_type) {
+    $available_types = array();
+    foreach ($relation_type->source_bundles as $bundle_key) {
+      list($entity_type, $bundle) = explode(':', $bundle_key, 2);
+      $available_types['source'][$entity_type][] = $bundle;
+    }
+    foreach ($relation_type->target_bundles as $bundle_key) {
+      list($entity_type, $bundle) = explode(':', $bundle_key, 2);
+      $available_types['target'][$entity_type][] = $bundle;
+    }
+    $arity = rand($relation_type->min_arity, $relation_type->min_arity);
+    for ($i = $number; $i > 0; $i--) { // start new relation
+      $entity_keys = array();
+      for ($r_index = 0; $r_index < $arity; $r_index++) {
+        if ($relation_type->directional && $r_index > 0) {
+          $direction = 'target';
+        }
+        else { //use source bundles
+          $direction = 'source';
+        }
+        $entity_type = array_rand($available_types[$direction]);
+        $query = new EntityFieldQuery();
+        $query->entityCondition('entity_type', $entity_type, '=')
+          ->range(0, 2 * $number);
+          // Would be nice to ->entityOrderBy('RAND()'); here, and set
+          // range(0, 1). See http://drupal.org/node/1174806
+        if (!in_array('*', $available_types[$direction][$entity_type])) {
+          $query->entityCondition('bundle', $available_types[$direction][$entity_type], 'IN');
+        }
+        $results = $query->execute();
+        $entity_ids = array_keys(reset($results));
+        $entity_id = array_rand($entity_ids); //pseudorandomise until EFQ does random.
+        $entity_keys[] = array(
+          'entity_type' => $entity_type,
+          'entity_id'   => $entity_id,
+          'r_index'     => $r_index,
+          );
+      }
+      $rids[] = relation_create($type, $entity_keys);
+    }
+  }
+  return $rids;
+}
