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 char *args[2] = { "", "" }, *s;
54 int min_int, response_int, max_int;
56 LOCAL_USER_ACF_ADD(u);
58 s = ast_strdupa(data);
60 ast_app_separate_args(s, '|', args, sizeof(args) / sizeof(args[0]));
62 if (ast_strlen_zero(args[0]) || sscanf(args[0], "%d", &min_int) != 1) {
66 if (ast_strlen_zero(args[1]) || sscanf(args[1], "%d", &max_int) != 1) {
70 if (max_int < min_int) {
74 ast_log(LOG_DEBUG, "max<min\n");
77 response_int = min_int + (ast_random() % (max_int - min_int + 1));
78 ast_log(LOG_DEBUG, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
79 snprintf(buffer, buflen, "%d", response_int);
88 struct ast_custom_function acf_rand = {
90 .synopsis = "Choose a random number in a range",
91 .syntax = "RAND([min][,max])",
93 "Choose a random number between min and max. Min defaults to 0, if not\n"
94 "specified, while max defaults to RAND_MAX (2147483647 on many systems).\n"
95 " Example: Set(junky=${RAND(1,8)}); \n"
96 " Sets junky to a random number between 1 and 8, inclusive.\n",
97 .read = acf_rand_exec,
103 static char *tdesc = "Generate a random number";
105 int unload_module(void)
107 ast_custom_function_unregister(&acf_rand);
109 STANDARD_HANGUP_LOCALUSERS;
114 int load_module(void)
116 return ast_custom_function_register(&acf_rand);
119 char *description(void)
128 STANDARD_USECOUNT(res);
135 return ASTERISK_GPL_KEY;