random function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M. Braun

    random function

    Hello,

    first I have to say, my english isn't very well so just try to understand it
    ;-).

    I've got a little problem with the PHP function rand().
    I want to create a little programm which simulate a "roulette" game (you
    know this stupid game you can play in a casino :) ).
    But it is impossible to simulate such a game with a simple rand() function.
    Of course I initilize the random generator with the program start, with
    srand().
    But fact is, that, in a roulette game, the probability for each game is the
    same (for example: 1/37 for a simple number like '4' or '28').
    So I tried to put 37 items in an array called numbers[] and start the random
    generator with numbers from 0 to 36 and echoed the item to the screen.
    But if I put this system into e loop and try to simulate about 1000 games I
    evidently get e "Laplace" experiment.
    But this is the fault. Statistic fails in a roulette game, I think. Do it?
    I think the ball has no "brain" so the probability is the same for each
    game. In fact it is possible that I get one number 50 times. But with the
    random generator, which is obviously too good, it won't work. What can I do?
    Thanks for help

    Martin


  • Chung Leong

    #2
    Re: random function

    I don't know enough about the pseudo-random algorithm to say for sure, that
    the result from one call doesn't somehow influence the result from the next.
    In theory, the results are independent (pseudo-independent, I should say).
    Run this little snippet and you'll see that rand() is perfectly capable of
    returning the same number consecutively:

    for($i = 0; $i = 100; $i++) {
    $n = rand(1, 4);
    echo "$n<br>";
    }

    And by the way, use mt_rand() instead. It's supposed to be better.

    Uzytkownik "M. Braun" <braun@uni-koblenz.de> napisal w wiadomosci
    news:bv0lo4$5us $1@news.uni-koblenz.de...[color=blue]
    > Hello,
    >
    > first I have to say, my english isn't very well so just try to understand[/color]
    it[color=blue]
    > ;-).
    >
    > I've got a little problem with the PHP function rand().
    > I want to create a little programm which simulate a "roulette" game (you
    > know this stupid game you can play in a casino :) ).
    > But it is impossible to simulate such a game with a simple rand()[/color]
    function.[color=blue]
    > Of course I initilize the random generator with the program start, with
    > srand().
    > But fact is, that, in a roulette game, the probability for each game is[/color]
    the[color=blue]
    > same (for example: 1/37 for a simple number like '4' or '28').
    > So I tried to put 37 items in an array called numbers[] and start the[/color]
    random[color=blue]
    > generator with numbers from 0 to 36 and echoed the item to the screen.
    > But if I put this system into e loop and try to simulate about 1000 games[/color]
    I[color=blue]
    > evidently get e "Laplace" experiment.
    > But this is the fault. Statistic fails in a roulette game, I think. Do it?
    > I think the ball has no "brain" so the probability is the same for each
    > game. In fact it is possible that I get one number 50 times. But with the
    > random generator, which is obviously too good, it won't work. What can I[/color]
    do?[color=blue]
    > Thanks for help
    >
    > Martin
    >
    >[/color]


    Comment

    • DvDmanDT

      #3
      Re: random function

      srand(time()%37 ); // change 37 to some other number depending on what
      chances you want...

      --
      // DvDmanDT
      MSN: dvdmandt¤hotmai l.com
      Mail: dvdmandt¤telia. com
      "Chung Leong" <chernyshevsky@ hotmail.com> skrev i meddelandet
      news:jbadnWd6QZ IdmYndRVn-uQ@comcast.com. ..[color=blue]
      > I don't know enough about the pseudo-random algorithm to say for sure,[/color]
      that[color=blue]
      > the result from one call doesn't somehow influence the result from the[/color]
      next.[color=blue]
      > In theory, the results are independent (pseudo-independent, I should say).
      > Run this little snippet and you'll see that rand() is perfectly capable of
      > returning the same number consecutively:
      >
      > for($i = 0; $i = 100; $i++) {
      > $n = rand(1, 4);
      > echo "$n<br>";
      > }
      >
      > And by the way, use mt_rand() instead. It's supposed to be better.
      >
      > Uzytkownik "M. Braun" <braun@uni-koblenz.de> napisal w wiadomosci
      > news:bv0lo4$5us $1@news.uni-koblenz.de...[color=green]
      > > Hello,
      > >
      > > first I have to say, my english isn't very well so just try to[/color][/color]
      understand[color=blue]
      > it[color=green]
      > > ;-).
      > >
      > > I've got a little problem with the PHP function rand().
      > > I want to create a little programm which simulate a "roulette" game (you
      > > know this stupid game you can play in a casino :) ).
      > > But it is impossible to simulate such a game with a simple rand()[/color]
      > function.[color=green]
      > > Of course I initilize the random generator with the program start, with
      > > srand().
      > > But fact is, that, in a roulette game, the probability for each game is[/color]
      > the[color=green]
      > > same (for example: 1/37 for a simple number like '4' or '28').
      > > So I tried to put 37 items in an array called numbers[] and start the[/color]
      > random[color=green]
      > > generator with numbers from 0 to 36 and echoed the item to the screen.
      > > But if I put this system into e loop and try to simulate about 1000[/color][/color]
      games[color=blue]
      > I[color=green]
      > > evidently get e "Laplace" experiment.
      > > But this is the fault. Statistic fails in a roulette game, I think. Do[/color][/color]
      it?[color=blue][color=green]
      > > I think the ball has no "brain" so the probability is the same for each
      > > game. In fact it is possible that I get one number 50 times. But with[/color][/color]
      the[color=blue][color=green]
      > > random generator, which is obviously too good, it won't work. What can I[/color]
      > do?[color=green]
      > > Thanks for help
      > >
      > > Martin
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...