Chapter 23. Miscellaneous

This chapter contains some commands, concepts, and thoughts that did not fit anywhere else but are still worth mentioning.

Random Numbers

It is occasionally useful to generate random numbers in Expect (e.g., playing a game such as the robohunt script does). There is no built-in command to provide random numbers, and it is worth contemplating why not and what to do about it, since the answer generalizes to other questions.

The philosophy of Expect is to not provide commands for which there are already numerous solutions. Furthermore, in the case of random numbers, there is no implementation of a random number generator (RNG) that will make everyone happy. Different RNGs make compromises between different goals, so your choice of random numbers depend on your reason for using them. Indeed, that is one reason why there are so many RNGs on UNIX.

If there were one RNG wired in to Expect, it would not make everyone happy. Many people care a great deal about the kind of random numbers they get and want to select a particular generator or write their own.

If an RNG is already written as a stand-alone program, run it using open or spawn. The choice of open or spawn allows you to get good performance whether your RNG is interactive or non-interactive. If it is non-interactive and generates a stream of random numbers, open it and use gets to read each new random number. If your RNG is interactive, send the appropriate command as needed and use expect to fetch the result. ...

Get Exploring Expect now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.