Choose numbers at random
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?
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
There isn't anything built-in.
For the former, you could do something like
For the latter, one option is
For the former, you could do something like
Code: Select all
(+ low (random (1+ (- high low))))
Code: Select all
(elt list (random (length list)))
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!
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!
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: Choose numbers at random
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 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 -_-
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?
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?
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: Choose numbers at random
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.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?
Re: Choose numbers at random
You should be able to do it in one traversal, at the cost of more calls to 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.
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))
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: Choose numbers at 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.qbg wrote:You should be able to do it in one traversal, at the cost of more calls to 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.
Re: Choose numbers at random
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 wrote: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.qbg wrote:You should be able to do it in one traversal, at the cost of more calls to 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.
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: Choose numbers at random
It is.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...".
