Choose numbers at random

Discussion of Common Lisp

Choose numbers at random

Postby mijokijo » Fri Dec 05, 2008 10:46 am

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?
mijokijo
 
Posts: 5
Joined: Fri Dec 05, 2008 10:35 am

Re: Choose numbers at random

Postby xach » Fri Dec 05, 2008 11:29 am

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)))
xach
 
Posts: 6
Joined: Tue Nov 11, 2008 7:48 am

Re: Choose numbers at random

Postby mijokijo » Fri Dec 05, 2008 1:28 pm

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!
mijokijo
 
Posts: 5
Joined: Fri Dec 05, 2008 10:35 am

Re: Choose numbers at random

Postby Paul Donnelly » Fri Dec 05, 2008 3:33 pm

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.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Choose numbers at random

Postby mijokijo » Fri Dec 05, 2008 3:48 pm

'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?
mijokijo
 
Posts: 5
Joined: Fri Dec 05, 2008 10:35 am

Re: Choose numbers at random

Postby Paul Donnelly » Fri Dec 05, 2008 11:08 pm

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.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Choose numbers at random

Postby qbg » Sat Dec 06, 2008 11:34 am

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))
qbg
 
Posts: 64
Joined: Mon Jun 30, 2008 1:05 pm
Location: Minnesota

Re: Choose numbers at random

Postby Paul Donnelly » Sat Dec 06, 2008 4:36 pm

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.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Choose numbers at random

Postby Wodin » Sun Dec 07, 2008 10:31 am

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...".
Wodin
 
Posts: 56
Joined: Sun Jun 29, 2008 8:16 am

Re: Choose numbers at random

Postby Paul Donnelly » Sun Dec 07, 2008 4:02 pm

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-)
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Next

Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests

cron