## Choose numbers at random

Discussion of Common Lisp
mijokijo
Posts: 5
Joined: Fri Dec 05, 2008 10:35 am

### Choose numbers at random

Wazzaaaaaa! I'm trying to create two different functions that choose numbers at random.

I want something like this: (ranged-random 1 10)

RANGED-RANDOM would simply pick a number given the range (inclusively)

I also want something like this: (list-random '(6 8 15 90 3 5))

LIST-RANDOM would pick one of the numbers in the list at random.

Are there any functions like these already, or will I have to make them myself?

xach
Posts: 6
Joined: Tue Nov 11, 2008 7:48 am

### Re: Choose numbers at random

There isn't anything built-in.

For the former, you could do something like

Code: Select all

``(+ low (random (1+ (- high low))))``
For the latter, one option is

Code: Select all

``(elt list (random (length list)))``

mijokijo
Posts: 5
Joined: Fri Dec 05, 2008 10:35 am

### Re: Choose numbers at random

Excellent!

The function for the range is clever, I would have been banging my head for hours on that one.

As for the second function, I should have paid closer attention in PCL when it talks about sequences and vectors. I was searching the Hyperspec for stuff related to numbers, when I should have been looking at sequences -_-

This will be most helpful. Thank you my nerd brother!

Paul Donnelly
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

### Re: Choose numbers at random

mijokijo wrote:Excellent!

The function for the range is clever, I would have been banging my head for hours on that one.

As for the second function, I should have paid closer attention in PCL when it talks about sequences and vectors. I was searching the Hyperspec for stuff related to numbers, when I should have been looking at sequences -_-
Keep in mind, that one will have to iterate over the list twice, once to get its length, and once to get the random element (apologies if you know this already). It's probably fine for your purposes, but if it seems slower than it should be, consider either not using a list or storing the list length somewhere for quick lookup.

mijokijo
Posts: 5
Joined: Fri Dec 05, 2008 10:35 am

### Re: Choose numbers at random

'Eeeeeeyyyyy!

While it probably will be fine for my purpose, if ever it should seem slower than it should be, tell me what a good alternative to storing the values in a list would be? Hash tables?

Paul Donnelly
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

### Re: Choose numbers at random

mijokijo wrote:'Eeeeeeyyyyy!

While it probably will be fine for my purpose, if ever it should seem slower than it should be, tell me what a good alternative to storing the values in a list would be? Hash tables?
If you wanted to quickly pick random items from a set of numbers, a vector might be the natural choice. Getting the length of a list requires traversing it to the end, and getting the nth element from a list requires another traversal to the element you want. A vector stores its size and elements can be looked up directly, so if you were to frequently pick random elements from a sequence, a vector might be a better choice. Xach's code will work on vectors too, if you feed it one.

qbg
Posts: 64
Joined: Mon Jun 30, 2008 1:05 pm
Location: Minnesota

### Re: Choose numbers at random

Paul Donnelly wrote: Keep in mind, that one will have to iterate over the list twice, once to get its length, and once to get the random element (apologies if you know this already). It's probably fine for your purposes, but if it seems slower than it should be, consider either not using a list or storing the list length somewhere for quick lookup.
You should be able to do it in one traversal, at the cost of more calls to random:

Code: Select all

``````(defun random-nth (list)
(let ((element (car list))
(n 2))
(dolist (e (cdr list))
(if (zerop (random n))
(setf element e))
(incf n))
element))
``````

Paul Donnelly
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

### Re: Choose numbers at random

qbg wrote:
Paul Donnelly wrote: Keep in mind, that one will have to iterate over the list twice, once to get its length, and once to get the random element (apologies if you know this already). It's probably fine for your purposes, but if it seems slower than it should be, consider either not using a list or storing the list length somewhere for quick lookup.
You should be able to do it in one traversal, at the cost of more calls to random:
I suspected someone would be along with a way to do it in one traversal, and I think I meant only that particular implementation when I referred to the necessity of iterating twice. Don't know why I would make a blanket statement like that.

Wodin
Posts: 56
Joined: Sun Jun 29, 2008 8:16 am

### Re: Choose numbers at random

Paul Donnelly wrote:
qbg wrote:
Paul Donnelly wrote: Keep in mind, that one will have to iterate over the list twice, once to get its length, and once to get the random element (apologies if you know this already). It's probably fine for your purposes, but if it seems slower than it should be, consider either not using a list or storing the list length somewhere for quick lookup.
You should be able to do it in one traversal, at the cost of more calls to random:
I suspected someone would be along with a way to do it in one traversal, and I think I meant only that particular implementation when I referred to the necessity of iterating twice. Don't know why I would make a blanket statement like that.
If it's any consolation, I took "that one" to mean "that solution" and not "Keep in mind that: one will have to...".

Paul Donnelly
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

### Re: Choose numbers at random

Wodin wrote:If it's any consolation, I took "that one" to mean "that solution" and not "Keep in mind that: one will have to...".
It is.