17

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?

  • 2
    The important take-away from these answers are that you generally don't get random numbers from most computers, vintage or otherwise. You get varying degrees of quality of pseudo-random numbers, often in an identical series for any given seed. – user12 May 17 '16 at 15:03
13

From here it looks like the official way is to use RND(N) to seed, where is N is a negative number.

But according to this you can also just copy a byte from a timer to the RND seed with:

POKE 280,PEEK(275)

If your program shows a title screen with a loop checking for a user keypress, you could:

  • Do X=RND(0) in the loop. This will put you at a random place in the sequence.

  • Set N to -1 then decrement it in the loop (maybe wrap somewhere to -1 to avoid overflow). Then X=RND(N) to seed. (You could put that in the loop but it would slightly reduce the range of N.)

  • 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. – Payton Byrd Apr 28 '16 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). – Nick Westgate Apr 28 '16 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). – user294 May 2 '16 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. – user12 Jun 11 '16 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. – Nick Westgate Jun 12 '16 at 22:54
2

Use a value derived from a CMOS clock for your seed, since those are never the same twice.

On an older computer with no CMOS clock, there will still be a clock that starts from zero every time the computer is started. You can use this clock as well, as long as the program start time relies on human action which will introduce a random delay. Manually starting the program should introduce enough error for a reliably unique seed. If you autostart the program with a zeroed-out clock on reboot, and without requiring any human input, then you are very likely to get repeating values for your seed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.