...

View Full Version : saving mysql order number



arne2
08-26-2006, 10:02 AM
HI,
i have created a script that randomly creates endtimes (of a race).
So like this :
$endtime1=mt_rand(1,5);
and so on for 5times (so till $endtime5).
Endtime1 is the endtime of horse nr 1
Endtime2....................................2,...

The endtimes are written in the database in the table 'horsescores'.
(id(auto_increment),horsenr,endtime,rank).
In horsenr the number of the horse (1-5)is written. In the endtime column the endtimes are written.
I now want to write the rank (depending on the endtimes, so the horse with the smallest endtime is the winner, etcetc untill the loser (5th).
How can i do this?
I thought about selecting all horses and order them by endtime with an sql query but i don't exactly now how i can save that ordering in ranknumbers.
And maybe there is a better way?

Thank you for you help

GJay
08-26-2006, 10:18 AM
the endtime and rank are the same piece of information, why do you need to store it twice?

arne2
08-26-2006, 10:19 AM
because i want to make the game look real by adding an endtime into the scorebord.
But you're probably right and think i just realized what you mean.
Can i (on the scorebord), order them by endtime ASC and adding the corresponding rank in the 'rank' column?
How?
With an $i ?
And when it starts $i = 1 (so first), and use a while loop with $i++ everytime?

GJay
08-26-2006, 11:16 AM
yep, sounds about right.

arne2
08-26-2006, 11:34 AM
cool ! I'll try it now !
About the mt_rand
is there a way to use decimal numbers, for example an mt_rand between 9,11 and 12,23?

Mwnciau
08-26-2006, 02:15 PM
I dont think so... you could do :



$whole = mt_rand(10, 20);
$dec = mt_rand(0, 99);

$number = $whole . '.' . $dec;

Mwnciau
08-26-2006, 02:24 PM
extending that:


function my_rand_dec($min, $max){
list ($minwhole, $mindec) = explode('.', $min, 2);
list ($maxwhole, $maxdec) = explode('.', $max, 2);
$whole = mt_rand($minwhole, $maxwhole);
$dec = mt_rand($mindec, $maxdec);
return $whole . '.' . $dec;
}

arne2
08-27-2006, 11:19 AM
thank you very much :):thumbsup:

marek_mar
08-27-2006, 04:07 PM
extending that:


function my_rand_dec($min, $max){
list ($minwhole, $mindec) = explode('.', $min, 2);
list ($maxwhole, $maxdec) = explode('.', $max, 2);
$whole = mt_rand($minwhole, $maxwhole);
$dec = mt_rand($mindec, $maxdec);
return $whole . '.' . $dec;
}

The decimal restrictions should only apply when the random number is either $minwhole or $maxwhole. When called with for ex. 20.8 and 30.9 you won't get a 25.1

Mwnciau
08-27-2006, 04:19 PM
ah yes... i see my flaw... but it will probably work for what he wants.

marek_mar
08-27-2006, 05:57 PM
How about like this:


<?php
function float_mt_rand($min, $max)
{
$i = 0;
$j = 0;
while((int) $min != (float) $min)
{
$min *= 10;
$i++;
}
while((int) $max != (float) $max)
{
$max *= 10;
$j++;
}
$i = max($i, $j);

return mt_rand($min, $max) / pow(10, $i);
}
var_dump(float_mt_rand(1.244, 93.1));
?>

The only flaw is that the numbers can't get too long unless yoy will use BCMath or GMP.

arne2
08-27-2006, 06:23 PM
Hi Mwnciau & marek_mar
thank you both for your efforts in helping me :)
About your code, Mwnciau, when i tried it it only returned decimal numbers of .0 and .1 so( 174.1,154.1,4564.1:456465.1,......)
so that's not really what i wanted.
About your code marek_mar, seems to work perfectly random, but it prints float() and int() with it, can you delete that? Thank you (because i just want to store the value in a variable like for example $randnr=float_mt_rand(min,max); for that it should only be a number right? (sorry i'm a total noob :p)

Also about your code marek_mar, when i tried it (with your example numbers of min and max), i almost all the time gotnumbers 0,985 and 1,6 and never like 13.2. Why? (i don't understand your function :p).
Also : i should have numbers varying between 9.12 and 14.1, should i just enter that numbers in the min and max?

THANK YOU BOTH AGAIN!

marek_mar
08-27-2006, 07:44 PM
About your code marek_mar, seems to work perfectly random, but it prints float() and int() with it, can you delete that? Thank you (because i just want to store the value in a variable like for example $randnr=float_mt_rand(min,max); for that it should only be a number right? (sorry i'm a total noob :p)

Yes, the int/float is from var_dump(). It is useful to know which type a variable has sometimes and that's why I use it.


Also about your code marek_mar, when i tried it (with your example numbers of min and max), i almost all the time gotnumbers 0,985 and 1,6 and never like 13.2. Why? (i don't understand your function :p).
I'm sorry this is a bug. This is version is corrected.


<?php
function float_mt_rand($min, $max)
{
$i = 0;
while((int) $min != (float) $min || (int) $max != (float) $max)
{
$min *= 10;
$max *= 10;
$i++;
}
return mt_rand($min, $max) / pow(10, $i);
}
var_dump(float_mt_rand(1.244, 93.1));
?>

thunderhoster
08-27-2006, 07:46 PM
This is not the best way to solve your problem (it can occasionally cause some delay) but the quickest:


<?php

function my_rand_dec($min, $max){
$whole = 0;
$dec = 0;

$maxwhole = explode('.', $max, 2);
$maxwhole = $maxwhole[0];

$minwhole = explode('.', $min, 2);
$minwhole = $minwhole[0];

while (($whole . '.' . $dec) == 0 || ($whole . '.' . $dec) > $max || ($whole . '.' . $dec) < $min) {
$whole = mt_rand($minwhole, $maxwhole);
$dec = mt_rand(0, 99);
}

return $whole . '.' . $dec;
}

?>

arne2
08-29-2006, 11:07 AM
thank you both i'm going to test it right away !
Could anyone of you comment your code because i tried but i can't really figure out what everything means.

arne2
08-29-2006, 12:03 PM
great ! It's working, thank you !

arne2
08-29-2006, 12:14 PM
Oops, noticed one small problem, how can i make sure none of the endtime's are the same (because then ranks should be the same too).

I've put the float_mt_rand function in 5variables called
$endtime1,
$endtime2,..

how can i make sure none of the $endtime's are the same?
THANK YOU VERY MUCH !!

marek_mar
08-29-2006, 03:27 PM
thank you both i'm going to test it right away !
Could anyone of you comment your code because i tried but i can't really figure out what everything means.
Mine just multiplies $min and $max by 10 untill both numbers are integers and uses them for mt_rand(). Then it divides the value by a power of 10 equal to the number of times $min and $max was multiplied by 10 before.

marek_mar
08-29-2006, 03:30 PM
You can generate the times so that the $min for the next random time is the time of the next fastest horse.

Mwnciau
08-29-2006, 03:35 PM
$endtime[] = float_mt_rand();
$endtime[] = float_mt_rand();
$endtime[] = float_mt_rand();
$endtime[] = float_mt_rand();
$endtime[] = float_mt_rand();

for($a = 0; $a < 5; $a++){
$b = 0;
$c = $a + 1;
while ($b < 5){
while ($endtime[$a] == $endtime[$c]){
$endtime[$a] = float_mt_rand();
}
$c = ($c == 5) ? 0 : $c + 1;
$b++;
}
}

That will do somechecking but wont be perfect... not even sure that will work :p

arne2
08-29-2006, 03:36 PM
i don't want to change anything about the $min and $max during the execution of the function.
I ONLy want them to be all different.
Any idea?

THANKS FOR EXPLAINING AND HELPING ME !

arne2
08-29-2006, 03:37 PM
mwnciau thank you for your response, but are you sure all endtimes are checked that way?

Mwnciau
08-29-2006, 03:55 PM
Yes, I'm pretty sure they are all tested but there is a possibility that the new ones will be the same (a lot less chance though I think).

Mwnciau
08-29-2006, 04:10 PM
How are you putting them into the database, is it a new row for each result? (ie. row for result1, and row for result2



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum