Page 1 of 2

Choose numbers at random

Posted: Fri Dec 05, 2008 10:46 am
by mijokijo
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?

Re: Choose numbers at random

Posted: Fri Dec 05, 2008 11:29 am
by xach
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)))

Re: Choose numbers at random

Posted: Fri Dec 05, 2008 1:28 pm
by mijokijo
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!

Re: Choose numbers at random

Posted: Fri Dec 05, 2008 3:33 pm
by Paul Donnelly
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.

Re: Choose numbers at random

Posted: Fri Dec 05, 2008 3:48 pm
by mijokijo
'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?

Re: Choose numbers at random

Posted: Fri Dec 05, 2008 11:08 pm
by Paul Donnelly
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.

Re: Choose numbers at random

Posted: Sat Dec 06, 2008 11:34 am
by qbg
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))

Re: Choose numbers at random

Posted: Sat Dec 06, 2008 4:36 pm
by Paul Donnelly
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.

Re: Choose numbers at random

Posted: Sun Dec 07, 2008 10:31 am
by Wodin
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...".

Re: Choose numbers at random

Posted: Sun Dec 07, 2008 4:02 pm
by Paul Donnelly
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. 8-)