Random

J

Jacob JKW

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
 
I

it_says_BALLS_on_your_forehead

Jacob said:
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.
 
X

xhoster

Jacob JKW said:
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
 
R

robic0

Jacob JKW said:
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.
 
R

robic0

Jacob said:
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?
 
J

Jacob JKW

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.
 
R

robic0

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!!
 
J

Jacob JKW

robic0 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top