markvaughn2006 Posted October 21, 2009 Share Posted October 21, 2009 On the rand() function is there a way to have it pick a random number between say... 1000-1025 AND 2000-2025 AND 3000-3025, etc... so possible outcomes could be 2014, 1023, 1005, 3002, etc... all from this one rand() I know thats weird , just have a specific reason for wanting to do this... Thanks in advance!! Quote Link to comment Share on other sites More sharing options...
salathe Posted October 21, 2009 Share Posted October 21, 2009 From one call to rand(), no. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 21, 2009 Share Posted October 21, 2009 I suppose you could always do like this: $rand = array(rand(1000,1025), rand(2000,2025), rand(3000,3025)); $num = $rand[rand(0,2)]; Edit: Or like this: $first = rand(1,3) * 1000; $num = rand($first, $first + 25); Quote Link to comment Share on other sites More sharing options...
Adam Posted October 21, 2009 Share Posted October 21, 2009 As salathe said there isn't with a single call to rand(), but you could use something like: $ranges = array( 0 => array(1000, 1025), 1 => array(2000, 2025), 2 => array(3000, 3025) ); $n = array_rand($ranges); echo rand($ranges[$n][0], $ranges[$n][1]); Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 21, 2009 Share Posted October 21, 2009 I'll throw in another solution: $values = array_merge(range(1000, 1025), range(2000, 2025), range(3000, 3025)); $rand_value = $values[array_rand($values)]; EDIT: MrAdam went with a similar solution, but mine picks a single random value out of the entire list instead of first selecting one of three lists and then selecting a value out of that. Quote Link to comment Share on other sites More sharing options...
markvaughn2006 Posted October 21, 2009 Author Share Posted October 21, 2009 cool thanks, that'll work!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.