By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Selection based on ID number

P: n/a
Hi

I'm trying to write a function that will accept an ID number and
depending on certain criteria (that's what I need help with) it will
return a number between a specified range (lets say 1-10). The thing is
it must return the same number every time so any random functions are
out the window. I was using modulos but that's only good for 2 numbers.

ID's could be any number at all but the most important thing is the
number that is selected from the range must be the same for the ID
every time the function is ran no matter if the range grows. That is if
today I run it with a range of 1-10 and then tomorrow I run it with a
range of 1-20 the number that was returned yesterday for ID=28, for
example, should be the same number that is returned today for ID=28

Hope I've manged to make that clear ;)

Thanks

S66

Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
This is some pseudo code that might make it clearer.

GetNumber(23)

function GetNumber(ID){

//this function will return a number within the range specified based
on the ID passed

//define range
$min = 1;
$max = 12;

$retValue = 'do something with the id number so that we can return a
value between the $min and $max values';

return $retValue;

}

Thanks

S66

Jul 17 '05 #2

P: n/a
On 3 Apr 2005 06:38:37 -0700, "sphincter66" <sp*********@hotmail.com>
wrote:
This is some pseudo code that might make it clearer.

GetNumber(23)

function GetNumber(ID){

//this function will return a number within the range specified based
on the ID passed

//define range
$min = 1;
$max = 12;

$retValue = 'do something with the id number so that we can return a
value between the $min and $max values';

return $retValue;

}

Thanks

S66


You could store the first instance of the ID for later use.. something
along the lines of:
function getNumber($id) {
return rand(1, 10);
}
$id = 23;
$num = getNumber($id);
$res = mysql_query("
INSERT INTO " . SOME_TABLE . "
(
id,
num
)
VALUES (
$id,
$num
)
");

[ do rest of SQL validation here ]

Then next time, check to see if id 23 in the DB has a value assigned, if
so, read and use that value, if not, call getNumber() to assign them an
ID.
I hope this is an example of what you're looking for =)

Regards,

Ian
--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #3

P: n/a
Thanks for the response Ian. I guess that would work too. I was kind of
looking for a function that would actually be able to calculate the
return value on the fly.

If I can't come up with that solution this will be my 'fall back' plan.

thanks again

S66

Jul 17 '05 #4

P: n/a
On 3 Apr 2005 06:58:30 -0700, "sphincter66" <sp*********@hotmail.com>
wrote:

Hi S..

Thanks for the response Ian. I guess that would work too. I was kind of
looking for a function that would actually be able to calculate the
return value on the fly.

If I can't come up with that solution this will be my 'fall back' plan.

thanks again

S66

No probs =)

The "solution" I came up with was my initial thinking from the limited
info I had. Could you explain a little more how this will all be used?
Is it member id based based on certain criteria? or something completely
different?

You could write a function that just returns a fixed range of say 0-10,
but I'm not seeing the relevence of feeding it an ID number if you want
the output to be "static". I guess this is due to me not knowing how
you're thinking of implementing it all =)

If you need to return the same number.. I think you will end up having
to use a "marker" of some description.. although there's bound to be
many different ways to achieve this.

Regards,

Ian
--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #5

P: n/a
Hi Ian

The relevance of feeding it the ID is that this is the unique
identifier for the number returned. What I mean is that I need to have
the same number returned from the range specified based on the ID
passed. That is if 23 is passed as the ID and the range is 1-10 the
return may be 3 for example. The next time the function is called I may
have changed the range to 1-25 but if the ID passed is 23 it must still
return 3.

Your solution using a database would work but as I say I was really
hoping to find a solution that would calculate the return value.

Hope that clarifys it a bit.

S66

Jul 17 '05 #6

P: n/a
I've been thinking about this for a few hours and I'm not sure it can
be done. I may have to go with Ians database solution.

S66

Jul 17 '05 #7

P: n/a
I don't think so either, and here's the reason:

You're taking an infinite set of inputs and mapping them to a finite
set of outputs. So, suppose every number you could possibly input gets
mapped to a number between 1 and 25. You send in every single number
in the universe and get your mapping. The next day, you change your
range to 1 - 30. But every number is already mapped to something
between 1 and 25. So there can't be anything that would map to a
number between 26 and 30, since all the mappings have already been
created.

Ian is correct in that you have to "remember" what got mapped to what
(using a database or some other means).

Jul 17 '05 #8

P: n/a
On 3 Apr 2005 07:32:34 -0700, "sphincter66" <sp*********@hotmail.com>
wrote:

Hi S..
Hi Ian

The relevance of feeding it the ID is that this is the unique
identifier for the number returned. What I mean is that I need to have
the same number returned from the range specified based on the ID
passed. That is if 23 is passed as the ID and the range is 1-10 the
return may be 3 for example. The next time the function is called I may
have changed the range to 1-25 but if the ID passed is 23 it must still
return 3.

Your solution using a database would work but as I say I was really
hoping to find a solution that would calculate the return value.

Hope that clarifys it a bit.

S66

Yup, it does.. but as you've since discovered, I don't think there is
going to be a way of doing this without storing a previous value if
assigned. There probably is, using some "extreme" code and lots of
maths, but none that I'm aware of without some major thought.

I know you've already pretty much had this said by Zeldor too.. but I
thought it only polite to at least reply =)

Hope whatever method you do finally use works out ok.

Regards,

Ian
--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.