For Programmers: Free Programming Magazines  


Home > Archive > Scheme > December 2005 > Defining a random range









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Defining a random range
johnofarc

2005-12-10, 7:15 pm

Hi everyone, That's my first post to this list, so please
forgive me if I don't know how to do the posting or so..
My question is actually very simple for you I'm sure but I'm new to the
random ability of scheme, in one of my projects, I try to create a
random range, I want to give to numbers to my function and it should
give me a number between these two, but a pseudo random number: Thats
what I wrote:
(define random-range
(lambda (n1 n2)
(cond
((<= (random n2) n1) (random-range n1 n2))
(else (random n2)))))
according to me that should simply check if a number smaller than n2 is
greater than n1 and if it is it takes that number, if it's not it
retrys the procedure? but the results I get show me I couldn't manage
it, can anyone tell me what the problem might be? Thanks a lot..

Jens Axel Søgaard

2005-12-10, 7:15 pm

johnofarc wrote:
> Hi everyone, That's my first post to this list, so please
> forgive me if I don't know how to do the posting or so..
> My question is actually very simple for you I'm sure but I'm new to the
> random ability of scheme, in one of my projects, I try to create a
> random range, I want to give to numbers to my function and it should
> give me a number between these two, but a pseudo random number: Thats
> what I wrote:
> (define random-range
> (lambda (n1 n2)
> (cond
> ((<= (random n2) n1) (random-range n1 n2))
> (else (random n2)))))


> according to me that should simply check if a number smaller than n2 is
> greater than n1 and if it is it takes that number, if it's not it
> retrys the procedure? but the results I get show me I couldn't manage
> it, can anyone tell me what the problem might be? Thanks a lot..


Hint: In (<= (random n2) n1) you check that the result of (random n2)
is smaller than n1. If it is you return (random n2). But is
very seldom you get the same random number twice in a row.
That is, you need to remember the random number, then check
whether is in the desired range.

--
Jens Axel Søgaard

johnofarc

2005-12-10, 7:15 pm

But I don't know the function that will let me keep (remember) the
random number,I'm given..
random is sometimes giving me the same result (remembers) the result it
gives but when is that the case? What shall I do to make random
remember what it gave me and gave it again?
Thank you very much for your response by the way! Thats my first in
this group but not the last for sure!

Jens Axel Søgaard

2005-12-10, 7:15 pm

johnofarc wrote:
> But I don't know the function that will let me keep (remember) the
> random number,I'm given..
> random is sometimes giving me the same result (remembers) the result it
> gives but when is that the case? What shall I do to make random
> remember what it gave me and gave it again?
> Thank you very much for your response by the way! Thats my first in
> this group but not the last for sure!


One way to do this without introducing new language constructs is:

(define random-range
(lambda (n1 n2)
(check-random-range n1 n2 (random n2)))

(define check-random-range
(lambda (n1 n2 r)
(cond
...)))

Another is to use "let".

--
Jens Axel Søgaard

johnofarc

2005-12-10, 7:15 pm

Hey thanks a lot the one without adding new constructs
seems to work perfectly and I understood it very well. Thank you very
much Jens!! But "let" may be useful for future use ;) Thanks again!

Giorgos Keramidas

2005-12-10, 10:10 pm

On 10 Dec 2005 16:18:16 -0800, "johnofarc" <mert.johnofarc@gmail.com> wrote:
> Hey thanks a lot the one without adding new constructs
> seems to work perfectly and I understood it very well. Thank you very
> much Jens!! But "let" may be useful for future use ;) Thanks again!


To make sure that (random n2) is called only once, you can use:

(define random-range
(lambda (n1 n2)
(let ((r (random n2)))
(cond ...)))

Inside the (cond) expression, you can use 'r' to refer to the randomly
chosen value, because 'r' is bound to that value in the entire body of
the (let) expression.

Ray Dillinger

2005-12-11, 7:18 pm

Giorgos Keramidas wrote:
> On 10 Dec 2005 16:18:16 -0800, "johnofarc" <mert.johnofarc@gmail.com> wrote:
>
>
>
> To make sure that (random n2) is called only once, you can use:
>
> (define random-range
> (lambda (n1 n2)
> (let ((r (random n2)))
> (cond ...)))
>
> Inside the (cond) expression, you can use 'r' to refer to the randomly
> chosen value, because 'r' is bound to that value in the entire body of
> the (let) expression.
>


I'm actually surprised that no one has suggested

(define random-range
(lambda (n1 n2)
(if (> n1 n2)
(random-range n2 n1)
(+ n1 (random (- n2 n1))))))

yet. Instead of filtering out a bunch of values, why
not just generate a random number that's *in* the
desired range instead? It's faster, especially if n1
and n2 are large and differ by only small percentage.
And there are no lets or conds, so it's easily do-able
without introducing new language constructs that a
beginner will not have seen.

Bear

johnofarc

2005-12-11, 7:18 pm

I will try out this one, actually I don't know the use of if yet as
well, but maybe this may be a good way to start learning it, besides
the way I tried to do first would get very nasty, if the number I look
for is in the range 9999995 10000000, but I got all the answer's I
was sing, plus I learned new things! Thank you very much

Förster vom Silberwald

2005-12-13, 8:15 am

btw: if you are after a good and sophisticated random number generator
with a lot of bells-and-whistles you might want to have a look at the
RngStream library at:

www.iro.umontreal.ca/~lecuyer/myftp/streams00/c/

I once posted a small and rough binding for Bigloo on the Bigloo
mailinglist. The C-functions are straightforward to include into your
particular Scheme environment I assume.

Schneewittchen

Bradley J Lucier

2005-12-13, 7:06 pm

In article <1134479480.062155.116020@g44g2000cwa.googlegroups.com>,
Förster vom Silberwald <chain_lube@hotmail.com> wrote:
>btw: if you are after a good and sophisticated random number generator
>with a lot of bells-and-whistles you might want to have a look at the
>RngStream library at:
>
>www.iro.umontreal.ca/~lecuyer/myftp/streams00/c/
>
>I once posted a small and rough binding for Bigloo on the Bigloo
>mailinglist. The C-functions are straightforward to include into your
>particular Scheme environment I assume.


I believe that this is basically what's in SRFI-27,
"Sources of Random Bits" by Sebastian Egner.

This SRFI is implemented by a fair number of Scheme implementations,
including Gambit-C 4.0 beta.

Brad















Förster vom Silberwald

2005-12-14, 7:58 am

You are right. I recall having accessed the RngStream-library link from
the srfi-27 document sites. However, at that time it was not clear for
me whether srfi-27 works from the start on under Bigloo. A binding was
faster for me in this particular respect.

I was looking for a better random number generator. RngStream makes me
produce some fast random numbers for a problem in atmospheric sciences
(Monte-Carlo search algorithm for an "aerosol model").

Schneewittchen

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com