Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Color BASIC RND function not so random

The RND() function for random numbers appears to give the same results each time the computer is reset. How do I seed the random number generator with a random number?

Answer*

Cancel
7
  • 5
    As an aside, if you are launching your game from an automated emulator startup, you probably will get a non-random result regardless of trying to use RND. The reason is that the RND function simply cannot make up a random number, it uses an algorithm based on how long the computer has been turned on. Unfortunately, when you start a program automatically with most emulators, you have a cycle-exact startup each time, which negates the use of RND. The best solution is some seed that comes from user input before the random number is used. Commented Apr 28, 2016 at 13:59
  • 2
    A good point, though some emulators might initialize the random seed or other sources of entropy (the timer mentioned above). E.g. the AppleWin Apple II emulator does this. On physical hardware there could be other sources of entropy: if you're loading from tape or disk there will be variation in the time to load which would affect timers. It's interesting that the CoCo ROM doesn't automatically change the seed whereas the Apple II does (increment while waiting for user input). Commented Apr 28, 2016 at 22:03
  • 1
    Seeding it from the timer (based on the 60Hz or 50 Hz vertical refresh clock) is what is recommended. I believe, with Extended BASIC, the recommended command was RND(-TIMER). Commented May 2, 2016 at 16:44
  • According to that link, using a negative number doesn't reseed any "better" than a number > 0, but (again, according to the link) it starts the pseudo-random sequence at the value so you can repeat a specific sequence with a known starting value. Interesting, if true. According to the Tandy Extended Basic manual, RND(0) gives you a series of p-random values between 0 and 1. This implies you can use it similarly to modern language "random" functions. Commented Jun 11, 2016 at 2:52
  • @jdv All PRNGs repeat a sequence when given a specific seed. In fact, there is only one (long) sequence, and any RND operation simply puts you at a different position in the sequence. Commented Jun 12, 2016 at 22:54