in reply to Re: Random Numbers
in thread Random Numbers

% is the modulo operator. If you're familiar with long division, it gives the remainder. It'll chop off the fractional portion, due to some internal conversion magic.

(int is probably faster, but TIMTOWTDI). That, and I don't think rand will ever return exactly 10, in this case.

Replies are listed 'Best First'.
RE: RE: Re: Random Numbers
by little_mistress (Monk) on Apr 10, 2000 at 07:09 UTC
    um you know actually i think your right ... it should be rand(10)%10 and i should have used srand to seed the rand function. But when it comes down to it in most cases what i find is that random enough numbers are enough. And its pretty impossible to get a "truely" random number from a computer. So I dont press the point. I could see however in cases where say you were doing some sort of research to select subjects at random to fill a number of cells. In that case you would want a better random algorhythm, but in most cases ... its really ok to just have a random enough number.

      | And its pretty impossible to get a "truely" random
      | number from a computer

      Actually, it is quite possible to do so and it is done quite frequently. A popular way is from timing events, such as a user's input into the keyboard. (It's generating the seed that is a real issue.) Using good old unreliable human input works quite nicely :).

      Point taken that it is good enough for this example (as long as srand is used, of course!)

        A popular way is from timing events, such as a user's input into the keyboard. (It's generating the seed that is a real issue.) Using good old unreliable human input works quite nicely :)

        That reminds me of some Javascript I wrote nearly 30 years ago.

        I wanted to display one of 3 images randomly. So, the image was dependent on when the script loaded. 0-19 seconds past the minute for one image, 20-39 seconds for the next, etc.

        On the same site, we had a gallery of nearly 100 (probably uncompressed) images - how slow must that have been on 33k dialup!!! Because I figured people wouldn't scroll to the bottom, I arranged that every hour the bottom image got moved to the top to ring the changes.