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 if (!(s = ast_strdupa(data))) {
64 ast_app_separate_args(s, '|', args, sizeof(args) / sizeof(args[0]));
66 if (ast_strlen_zero(args[0]) || sscanf(args[0], "%d", &min_int) != 1) {
70 if (ast_strlen_zero(args[1]) || sscanf(args[1], "%d", &max_int) != 1) {
74 if (max_int < min_int) {
78 ast_log(LOG_DEBUG, "max<min\n");
81 response_int = min_int + (ast_random() % (max_int - min_int + 1));
82 ast_log(LOG_DEBUG, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
83 snprintf(buffer, buflen, "%d", response_int);
92 struct ast_custom_function acf_rand = {
94 .synopsis = "Choose a random number in a range",
95 .syntax = "RAND([min][,max])",
97 "Choose a random number between min and max. Min defaults to 0, if not\n"
98 "specified, while max defaults to RAND_MAX (2147483647 on many systems).\n"
99 " Example: Set(junky=${RAND(1,8)}); \n"
100 " Sets junky to a random number between 1 and 8, inclusive.\n",
101 .read = acf_rand_exec,
107 static char *tdesc = "Generate a random number";
109 int unload_module(void)
111 ast_custom_function_unregister(&acf_rand);
113 STANDARD_HANGUP_LOCALUSERS;
118 int load_module(void)
120 return ast_custom_function_register(&acf_rand);
123 char *description(void)
132 STANDARD_USECOUNT(res);
139 return ASTERISK_GPL_KEY;