2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Digium, Inc.
5 * Copyright (C) 2006, Claude Patry
7 * See http://www.asterisk.org for more information about
8 * the Asterisk project. Please do not directly contact
9 * any of the maintainers of this project for assistance;
10 * the project provides a web site, mailing lists and IRC
11 * channels for your use.
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License Version 2. See the LICENSE file
15 * at the top of the source tree.
20 * \brief Generate Random Number
22 * \author Claude Patry <cpatry@gmail.com>
23 * \author Tilghman Lesher ( http://asterisk.drunkcoder.com/ )
29 #include <sys/types.h>
33 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7682 $") */
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/app.h"
41 #include "asterisk/module.h"
50 static char *acf_rand_exec(struct ast_channel *chan, char *cmd, char *data, char *buffer, size_t buflen)
53 int min_int, response_int, max_int;
56 AST_DECLARE_APP_ARGS(args,
61 if (!(parse = ast_strdupa(data))) {
66 LOCAL_USER_ACF_ADD(u);
68 AST_STANDARD_APP_ARGS(args, parse);
70 if (ast_strlen_zero(args.min) || sscanf(args.min, "%d", &min_int) != 1) {
75 if (ast_strlen_zero(args.max) || sscanf(args.max, "%d", &max_int) != 1) {
79 if (max_int < min_int) {
83 ast_log(LOG_DEBUG, "max<min\n");
86 response_int = min_int + (ast_random() % (max_int - min_int + 1));
87 ast_log(LOG_DEBUG, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
88 snprintf(buffer, buflen, "%d", response_int);
97 struct ast_custom_function acf_rand = {
99 .synopsis = "Choose a random number in a range",
100 .syntax = "RAND([min][,max])",
102 "Choose a random number between min and max. Min defaults to 0, if not\n"
103 "specified, while max defaults to RAND_MAX (2147483647 on many systems).\n"
104 " Example: Set(junky=${RAND(1,8)}); \n"
105 " Sets junky to a random number between 1 and 8, inclusive.\n",
106 .read = acf_rand_exec,
112 static char *tdesc = "Generate a random number";
114 int unload_module(void)
116 ast_custom_function_unregister(&acf_rand);
118 STANDARD_HANGUP_LOCALUSERS;
123 int load_module(void)
125 return ast_custom_function_register(&acf_rand);
128 char *description(void)
137 STANDARD_USECOUNT(res);
144 return ASTERISK_GPL_KEY;