Author Topic: probability in php  (Read 750 times)

0 Members and 1 Guest are viewing this topic.

Offline nrsh_ramTopic starter

  • Enthusiast
  • Posts: 54
    • View Profile
probability in php
« on: February 07, 2010, 07:13:22 PM »
Hi there,

I need a help whereby i am wondering if SQL can perform calculation on statistics?

i am looking an sql command to genarate all the sample space ..for an example the user key in 123..
so the sql command muct be able to generate the sample space

{123,132,213,2}

is this can be done in sql?
kindly advice.

Offline RussellReal

  • Addict
  • Posts: 1,730
  • Gender: Male
    • View Profile
Re: probability in php
« Reply #1 on: February 08, 2010, 01:59:45 AM »
do you mean.. for 123

123
213
221
132
312
321
231
?

then no :)

BUT before you insert it into a row you can calculate it yourself..

or when you retrieve it you can calculate it..
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: probability in php
« Reply #2 on: February 08, 2010, 02:30:33 AM »
then no :)

Actually yes... same way you can drive nails into a wall with a screwdriver....
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline Gamic

  • Enthusiast
  • Posts: 100
  • Gender: Male
    • View Profile
Re: probability in php
« Reply #3 on: February 08, 2010, 03:24:23 AM »
Just for fun:
Code: [Select]
create table #test(
n int
);
insert into #test(n)
values (1);
insert into #test(n)
values (2);
insert into #test(n)
values (3);

select
distinct convert(char(1),t1.n) + convert(char(1), t2.n) + convert(char(2), t2.n) as 'output'
from #test t1
cross join #test t2
cross join #test t3
order by 1 asc ;
Result:
Code: [Select]
output
------
111
122
133
211
222
233
311
322
333

(9 row(s) affected)

Seemed a lot easier than driving a nail into a wall ;) - but will become a lot more annoying when you need to add extra numbers
Gamic
Don't forget that you can ask questions in the phpfreaks irc channel.
Server: irc.phpfreaks.com
Channel: ##help
mySql cheat sheet | SQL Server Cheat Sheet

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: probability in php
« Reply #4 on: February 08, 2010, 03:39:11 AM »
And what these numbers represent?
Where are 123,132,213 as demanded by OP? :P

Code: [Select]
SELECT
   t1.n,t2.n,t3.n
FROM test AS t1
   CROSS JOIN test AS t2
  ON t1.n != t2.n
   CROSS JOIN test AS t3
  ON t1.n != t3.n AND t2.n != t3.n;
« Last Edit: February 08, 2010, 03:46:09 AM by Mchl »
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline RussellReal

  • Addict
  • Posts: 1,730
  • Gender: Male
    • View Profile
Re: probability in php
« Reply #5 on: February 08, 2010, 06:06:48 AM »
Hi there,

I need a help whereby i am wondering if SQL can perform calculation on statistics?

i am looking an sql command to genarate all the sample space ..for an example the user key in 123..
so the sql command muct be able to generate the sample space

{123,132,213,2}

is this can be done in sql?
kindly advice.

however, I'm sure you can define a mysql function for this. but it is so pointless to have mysql do this in the first place, and insert and drop and create tables for each page view.. I shouldn't have said it was impossible, but thats alot more work then just doing it in php..
Like my help and want to hire me? Email me or add me to MSN at RussellonMSN@hotmail.com

Offline Gamic

  • Enthusiast
  • Posts: 100
  • Gender: Male
    • View Profile
Re: probability in php
« Reply #6 on: February 08, 2010, 05:03:06 PM »
Those numbers have gone for a walk :P.

But yea, just goes to show how silly one little typo can be.

Code: [Select]
create table #test(
n int
);
insert into #test(n)
values (1);
insert into #test(n)
values (2);
insert into #test(n)
values (3);

select
distinct convert(char(1),t1.n) + convert(char(1), t2.n) + convert(char(1), t3.n) as 'output' -- typo last time was t2.n being used twice... silly me!
from #test t1
cross join #test t2
cross join #test t3
order by 1 asc ;

drop table #test;

And this now produces the much better output:
Code: [Select]
output
------
111
112
113
121
122
123 //Here's one!
131
132 //Ah, This one was missing too!
133
211
212
213 //And you!
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333  //Only half evil!
« Last Edit: February 08, 2010, 05:04:26 PM by Gamic »
Gamic
Don't forget that you can ask questions in the phpfreaks irc channel.
Server: irc.phpfreaks.com
Channel: ##help
mySql cheat sheet | SQL Server Cheat Sheet

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: probability in php
« Reply #7 on: February 08, 2010, 05:05:37 PM »
Yeah. Now we have solutions for both with- and without- repetition permutations. A recursive function in PHP could be written for both.
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading