Random value with condition

This is a discussion on Random value with condition within the MySQL Database forums, part of the Database Forums category; I need to generate a random value from x to y having increment i. where x is start value, y ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-26-2008
Cosmic programmer
 
Posts: n/a
Default Random value with condition


I need to generate a random value from x to y having increment i.
where x is start value, y is end value.

so if x = 2 and y = 12 and i = 2

then random value should be generated from the following set

(2,4,6,8,10,12)

and if x = 1 then random number should be picked from the following set

(1,3,5,7,9,11)

I need to know both in MySQL and PHP and will choose whichever is simpler.

Thanks.



Reply With Quote
  #2 (permalink)  
Old 04-26-2008
Michael Fesser
 
Posts: n/a
Default Re: Random value with condition

..oO(Cosmic programmer)

>I need to generate a random value from x to y having increment i.
>where x is start value, y is end value.
>
>so if x = 2 and y = 12 and i = 2
>
>then random value should be generated from the following set
>
>(2,4,6,8,10,12)
>
>and if x = 1 then random number should be picked from the following set
>
>(1,3,5,7,9,11)
>
>I need to know both in MySQL and PHP and will choose whichever is simpler.


Read about 'for' loops.

Micha
Reply With Quote
  #3 (permalink)  
Old 04-26-2008
Cosmic programmer
 
Posts: n/a
Default Re: Random value with condition


"Cosmic programmer" <not-my-email@nowhere.com> wrote in message
news:fuum46$9as$1@registered.motzarella.org...
>
> I need to generate a random value from x to y having increment i.
> where x is start value, y is end value.
>
> so if x = 2 and y = 12 and i = 2
>
> then random value should be generated from the following set
>
> (2,4,6,8,10,12)
>
> and if x = 1 then random number should be picked from the following set
>
> (1,3,5,7,9,11)
>
> I need to know both in MySQL and PHP and will choose whichever is simpler.
>
> Thanks.
>
>


Got it! in MySQL

start_val + inc * floor(rand()*(floor((end_val-start_val)/inc + 1)))

I'm a genious!!!


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:15 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0