-1

I am trying to get a random number of 0-20 like so

RandomRange(0, 20)

I know alot of software when using there built in function for random it will give the same random numbers each time the program is ran, thus not so random.. Does RandomRange act this way? I could not test as not near programming computer. If Answer is yes, then how can i get a Really Random number?

Thanks

Glen Morse
  • 2,437
  • 8
  • 51
  • 102
  • 2
    "I could not test as not near programming computer." Well, you wouldn't need the answer until then either, would you? – GolezTrol Jul 20 '12 at 21:34
  • not true, i usually try to write code using notepad++ while at work and then i can take it home and try it. – Glen Morse Jul 21 '12 at 14:57

1 Answers1

4

http://www.delphibasics.co.uk/RTL.asp?Name=Random

http://www.delphibasics.co.uk/RTL.asp?Name=Randomize

The Randomize command will re-seed the random number generator based on the current time of day. With that, the only way you'll get the exact same sequence of "random" numbers is if you run the program at exactly the same time of day (usually measured down to fractions of a second for these kinds of purposes).

EDIT: You can also use RandSeed (http://www.delphibasics.co.uk/RTL.asp?Name=RandSeed) to select the seed yourself. This is useful if you want to test the same sequence multiple times, for debugging, or want to randomize based on some other seed than time of day.

DGH
  • 11,189
  • 2
  • 23
  • 24
  • so before i call RandomRange() , I just use Randomize; Glad i asked now i did not see that before! Thanks – Glen Morse Jul 20 '12 at 21:00
  • depending on your application, this could potentially be a flawed strategy. if it is a security thing, then time-based PRNG seeds are vulnerable because there is some level of predictability. Of course the level of risk depends on your application. – mfsiega Jul 20 '12 at 21:02
  • its for a game, its using it as a 20 sided dice – Glen Morse Jul 20 '12 at 21:10
  • 4
    @GlenMorse: Yeah, for a game application, this is plenty random enough. Also, remember that you only need to call Randomize once per run of the program. There's no need to call it each and every time you generate a random number, once the first seed has been set. – DGH Jul 20 '12 at 21:39
  • See also: http://stackoverflow.com/questions/3946869/how-reliable-is-the-random-function-in-delphi – Gerry Coll Jul 20 '12 at 23:08