Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Random

Reply
Thread Tools

Random

 
 
Jacob JKW
Guest
Posts: n/a
 
      04-11-2006
If this has already been asked and answered somewhere before, I can't
find where.

Let's say I have a sorted array of real numbers between 0 and 1. What
do you think would be the fastest way of determining between which two
indexes a randomly generated number lay?

Specifically, what would be a faster way to do this:

#!perl

my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
my $rand = rand();

foreach my $i (0 .. $#$cmf_ra) {
(print $i and last) if $rand <= $cmf_ra->[$i];
}


Thanks,
Jacob

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your_forehead
Guest
Posts: n/a
 
      04-12-2006

Jacob JKW wrote:
> If this has already been asked and answered somewhere before, I can't
> find where.
>
> Let's say I have a sorted array of real numbers between 0 and 1. What
> do you think would be the fastest way of determining between which two
> indexes a randomly generated number lay?
>
> Specifically, what would be a faster way to do this:
>
> #!perl
>
> my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
> my $rand = rand();
>
> foreach my $i (0 .. $#$cmf_ra) {
> (print $i and last) if $rand <= $cmf_ra->[$i];
> }


binary search.

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-12-2006
"Jacob JKW" <(E-Mail Removed)> wrote:
> If this has already been asked and answered somewhere before, I can't
> find where.
>
> Let's say I have a sorted array of real numbers between 0 and 1. What
> do you think would be the fastest way of determining between which two
> indexes a randomly generated number lay?


The fastest way would be to do it in C. Don't forget to turn on the -O3
flag, it makes a huge difference in these cases. Also, put a sentinel
value at the end of the list so that you don't need to test two conditions
on each iteration.

>
> Specifically, what would be a faster way to do this:
>
> #!perl
>
> my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
> my $rand = rand();
>
> foreach my $i (0 .. $#$cmf_ra) {
> (print $i and last) if $rand <= $cmf_ra->[$i];
> }


Do you really have only 6 things in the search list?
I think a linear search would be faster than a binary search when you
only have 6 things in the search list.

Implement it both ways and see.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      04-12-2006
On 12 Apr 2006 00:24:42 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>"Jacob JKW" <(E-Mail Removed)> wrote:
>> If this has already been asked and answered somewhere before, I can't
>> find where.
>>
>> Let's say I have a sorted array of real numbers between 0 and 1. What
>> do you think would be the fastest way of determining between which two
>> indexes a randomly generated number lay?

>
>The fastest way would be to do it in C. Don't forget to turn on the -O3
>flag, it makes a huge difference in these cases. Also, put a sentinel
>value at the end of the list so that you don't need to test two conditions
>on each iteration.
>
>>
>> Specifically, what would be a faster way to do this:
>>
>> #!perl
>>
>> my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
>> my $rand = rand();
>>
>> foreach my $i (0 .. $#$cmf_ra) {
>> (print $i and last) if $rand <= $cmf_ra->[$i];
>> }

>
>Do you really have only 6 things in the search list?
>I think a linear search would be faster than a binary search when you
>only have 6 things in the search list.
>

Yes you are right fag head, IF AND ONLY IF, the number of elements are
less than 5. Statististacalcallyy, this wins.
Yes binary search fag head. Look for my posts on binary search routine
optimizations.
 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      04-12-2006
On 11 Apr 2006 17:00:26 -0700, "it_says_BALLS_on_your_forehead" <(E-Mail Removed)> wrote:

>
>Jacob JKW wrote:
>> If this has already been asked and answered somewhere before, I can't
>> find where.
>>
>> Let's say I have a sorted array of real numbers between 0 and 1. What
>> do you think would be the fastest way of determining between which two
>> indexes a randomly generated number lay?
>>
>> Specifically, what would be a faster way to do this:
>>
>> #!perl
>>
>> my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
>> my $rand = rand();
>>
>> foreach my $i (0 .. $#$cmf_ra) {
>> (print $i and last) if $rand <= $cmf_ra->[$i];
>> }

>
>binary search.

How about a binocular search?

 
Reply With Quote
 
Jacob JKW
Guest
Posts: n/a
 
      04-12-2006
(E-Mail Removed) wrote:
> "Jacob JKW" <(E-Mail Removed)> wrote:
> > If this has already been asked and answered somewhere before, I can't
> > find where.
> >
> > Let's say I have a sorted array of real numbers between 0 and 1. What
> > do you think would be the fastest way of determining between which two
> > indexes a randomly generated number lay?

>
> The fastest way would be to do it in C. Don't forget to turn on the -O3
> flag, it makes a huge difference in these cases.

I hear you, but I want to get this done somewhat quickly and I'm
unfortunately not really what you'd call a "strong" C programmer.

> Also, put a sentinel
> value at the end of the list so that you don't need to test two conditions
> on each iteration.

Yeah, I know. I just acciudently left that out of my code sample.

> > Specifically, what would be a faster way to do this:
> >
> > #!perl
> >
> > my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
> > my $rand = rand();
> >
> > foreach my $i (0 .. $#$cmf_ra) {
> > (print $i and last) if $rand <= $cmf_ra->[$i];
> > }

>
> Do you really have only 6 things in the search list?
> I think a linear search would be faster than a binary search when you
> only have 6 things in the search list.

I've got 18 items in the list.

I Google'd "binary search" and dound some sample code at
http://staff.washington.edu/jon/dsa-perl/bsearch. I'm to implement and
see how it compares.

Many thanks for the reply,
Jacob.

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      04-12-2006
On 11 Apr 2006 17:51:56 -0700, "Jacob JKW" <(E-Mail Removed)> wrote:

>(E-Mail Removed) wrote:
>> "Jacob JKW" <(E-Mail Removed)> wrote:
>> > If this has already been asked and answered somewhere before, I can't
>> > find where.
>> >
>> > Let's say I have a sorted array of real numbers between 0 and 1. What
>> > do you think would be the fastest way of determining between which two
>> > indexes a randomly generated number lay?

>>
>> The fastest way would be to do it in C. Don't forget to turn on the -O3
>> flag, it makes a huge difference in these cases.

>I hear you, but I want to get this done somewhat quickly and I'm
>unfortunately not really what you'd call a "strong" C programmer.
>
>> Also, put a sentinel
>> value at the end of the list so that you don't need to test two conditions
>> on each iteration.

>Yeah, I know. I just acciudently left that out of my code sample.
>
>> > Specifically, what would be a faster way to do this:
>> >
>> > #!perl
>> >
>> > my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
>> > my $rand = rand();
>> >
>> > foreach my $i (0 .. $#$cmf_ra) {
>> > (print $i and last) if $rand <= $cmf_ra->[$i];
>> > }

>>
>> Do you really have only 6 things in the search list?
>> I think a linear search would be faster than a binary search when you
>> only have 6 things in the search list.

>I've got 18 items in the list.
>
>I Google'd "binary search" and dound some sample code at
>http://staff.washington.edu/jon/dsa-perl/bsearch. I'm to implement and
>see how it compares.
>
>Many thanks for the reply,
>Jacob.


Well, I geuss your off to the races with your new found knowledge.
Good luck, have a nice day!!
 
Reply With Quote
 
Jacob JKW
Guest
Posts: n/a
 
      04-12-2006

robic0 wrote:
> On 11 Apr 2006 17:51:56 -0700, "Jacob JKW" <(E-Mail Removed)> wrote:
>
> >(E-Mail Removed) wrote:
> >> "Jacob JKW" <(E-Mail Removed)> wrote:
> >> > If this has already been asked and answered somewhere before, I can't
> >> > find where.
> >> >
> >> > Let's say I have a sorted array of real numbers between 0 and 1. What
> >> > do you think would be the fastest way of determining between which two
> >> > indexes a randomly generated number lay?
> >>
> >> The fastest way would be to do it in C. Don't forget to turn on the -O3
> >> flag, it makes a huge difference in these cases.

> >I hear you, but I want to get this done somewhat quickly and I'm
> >unfortunately not really what you'd call a "strong" C programmer.
> >
> >> Also, put a sentinel
> >> value at the end of the list so that you don't need to test two conditions
> >> on each iteration.

> >Yeah, I know. I just acciudently left that out of my code sample.
> >
> >> > Specifically, what would be a faster way to do this:
> >> >
> >> > #!perl
> >> >
> >> > my $cmf_ra = [0, 0.25, 0.33, 0.625, 0.9, 0.95];
> >> > my $rand = rand();
> >> >
> >> > foreach my $i (0 .. $#$cmf_ra) {
> >> > (print $i and last) if $rand <= $cmf_ra->[$i];
> >> > }
> >>
> >> Do you really have only 6 things in the search list?
> >> I think a linear search would be faster than a binary search when you
> >> only have 6 things in the search list.

> >I've got 18 items in the list.
> >
> >I Google'd "binary search" and dound some sample code at
> >http://staff.washington.edu/jon/dsa-perl/bsearch. I'm to implement and
> >see how it compares.
> >
> >Many thanks for the reply,
> >Jacob.

>
> Well, I geuss your off to the races with your new found knowledge.
> Good luck, have a nice day!!

If you have any constructive advice I'd love to hear it.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random "The IListSource does not contain any datasources" and more (Crashing a live site at random, twice a week or so) Lars-Erik Aabech ASP .Net 8 04-28-2005 07:52 AM
Random not really random... Maziar Aflatoun ASP .Net 4 08-05-2004 01:26 AM
Random NOt random? Darren Clark ASP .Net 3 06-24-2004 05:23 PM



Advertisments