三元锂电池池RVVALUE和CCVVALUE

Nginx模块开发(四)
目标:能在配置和代码中使用变量。
配置中的变量
nginx的配置中随时随地都能看到变量的身影。最简单的,请看access_log:
log_format&
proxyformat&
"$remote_addr $request_time $http_x_readtime [$time_local]
\"$request_method http://$host$request_uri\" $status
$body_bytes_sent \"$http_referer\"
\"$http_user_agent\"";
就这个简单的日志格式,就直接使用了11个nginx变量。这些变量都是nginx本身提供的。嗯,确实,没有Tengine新增的变量,可惜了。如果使用Tengine的话,可以变个戏法:不要以为access_log只有格式定义可以使用变量,access_log的log文件里面也可以使用变量:
access_log
$year-$month-$day-access.
open_log_file_cache
上面这个配置使用了Tengine新增的几个变量,它能实现的功能的就是自动日志轮转。我测试过这种原生的nginx日志轮转,qps无变化,cpu利用率会上升5%,效果还行,关键是它不需要新建进程,比起crontab,named pipe等解决方案,它更容易部署。
在配置文件中,可以直接使用set指令来创建变量。一定要在使用变量以前声明变量,这在任何语言中都是必须遵守的,配置nginx也是这样,使用某个变量以前必须保证该变量可以被识别,否则nginx会认为配置有问题。达到这个目标可以使用前面提到的set指令。在使用set时,你需要传递变量值。变量值可以是静态的数字或者字符串,也可以是含有变量的字符串。无论是静态的还是动态的变量值,nginx都会用相同的方式来处理——在每个请求到来时,将每个变量的值计算一遍。这个处理方式与后面的Upstream的处理方式是不一样的。
除了set指令,还有其他一些方法来创建变量。有些模块,比如geo模块,map模块等等,都会创建变量,并且有一个类似switch的逻辑用来确定每个请求的变量值。
geo& $geo& { # the variable
created is $geo
& default&&&&&&&&&
127.0.0.1/32&&&&
192.168.1.0/24&&
10.1.0.0/16&&&&&
另外,命名的正则表达式捕获组也可以创建变量,比如
server_name&&
~^(www\.)?(?&domain&.+)$;
会创建一个变量$domain。而普通的正则表达式捕获组可以创建$0、$1、…、$9这10个变量。$0表示原串,$1-$9表示第一到第九个匹配组的内容。
免费的午餐,不用任何手工劳动就能使用的变量——nginx帮你创建了。上面log_format中使用的变量全部是内建变量。有几个特殊的内建变量族,现在列举一下:
cookie_XXX
对应cookie中的XXX参数
对应uri中的XXX参数
对应http请求头中的XXX字段
sent_http_XXX
对应http响应头中的XXX字段
upstream_http_XXX
对应nginx传递给后端服务器的请求头中的XXX字段
有些人经常问我统计请求的响应时间用哪个变量,答案是$request_time;更多人问我统计后端处理请求的时间用哪个变量,答案是$upstream_response_time;如果你问我客户端ip怎么获得,拜托,$remote_addr,上面的log_format都写着呢。开发工程师需要有点思考能力,最好来点想象力,再加点行动力。看文档固然轻松,就不能动动手试一下么?怎么行动?看nginx代码,只有这个是根本。
nginx的内建变量有90%都是在ngx_variables.c定义的,剩下的跟在各个功能实现模块里面。我们下面介绍如何在程序中创建和使用变量,然后大家就可以开怀的看nginx的源代码了。
Coding with
An example
nginx自己的map模块是个很经典的例子,我们来看一看如果在代码中创建和使用nginx变量。
先简单介绍一下map的使用吧。
map可以将一个变量映射到另一个变量,而且支持正则表达式。有些问题用map解决会非常优雅。具体参考:。
map $cookie_cna $dest {
&& default&&&&& xmatch0;
&& ~[a-z0-4\+]$ xmatch1;
&& ~[A-Z5-9\/]$ xmatch2;
上面这个例子的意思是根据变量$cookie_cna的值创建一个新的变量$dest。创建规则是$cookie_cna的值匹配正则表达式/~[a-z0-4\+]$/,$dest为“xmatch1”;$cookie_cna的值匹配正则表达式/~[A-Z5-9\/]$/,$dest为“xmatch2”;其他情况下,$dest为“xmatch0”。
下面开始分析nginx怎么实现这个功能的(nginx版本:1.0.8),包括block解析、创建变量、变量回调函数、直接引用变量和参数值中引用变量。
{ ngx_string("map"),
NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
ngx_http_map_block,
NGX_HTTP_MAIN_CONF_OFFSET,
以上是指令定义,“map”的处理函数是ngx_http_map_block。这个函数和我们之前看到的“hello_world”的处理函数不同。前者需要处理一个块,后者仅需处理当前行。nginx的http {}、server {}、location{}、event {},这些都是块。
static char *
ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void
ngx_http_map_conf_t& *mcf =
char&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&*
ngx_conf_t&&&&&&&&&&&&&&&&&&&&&&&&
ngx_pool_t&&&&&&&&&&&&&&&&&&&&&&&
ngx_http_map_ctx_t&&&&&&&&&&&&&&&
ngx_http_map_conf_ctx_t&&&&&&&&&&&
if (mcf-&hash_max_size == NGX_CONF_UNSET_UINT)
mcf-&hash_max_size = 2048;
map = ngx_pcalloc(cf-&pool,
sizeof(ngx_http_map_ctx_t));
if (map == NULL) {
return NGX_CONF_ERROR;
name = value[2];
name.len--;
name.data++;
var = ngx_http_add_variable(cf, &name,
NGX_HTTP_VAR_CHANGEABLE);
if (var == NULL) {
return NGX_CONF_ERROR;
var-&get_handler = ngx_http_map_
var-&data = (uintptr_t)
pool = ngx_create_pool(16384, cf-&log);
if (pool == NULL) {
return NGX_CONF_ERROR;
ctx.keys.pool = cf-&
ctx.keys.temp_pool =
if (ngx_hash_keys_array_init(&ctx.keys,
NGX_HASH_LARGE) != NGX_OK) {
ngx_destroy_pool(pool);
return NGX_CONF_ERROR;
cf-&pool =
cf-&ctx = &
cf-&handler = ngx_http_
cf-&handler_conf =
rv = ngx_conf_parse(cf, NULL);
if (rv != NGX_CONF_OK) {
ngx_destroy_pool(pool);
map-&default_value = ctx.default_value ?
ctx.default_value:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&ngx_http_variable_null_
ngx_destroy_pool(pool);
上面是一个简化的ngx_http_map_block。大体上一个块解析需要完成下面几件工作:
创建变量使用ngx_http_add_variable函数。
ngx_http_variable_t
ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name,
ngx_uint_t flags);
其中name是变量的名称
flags是变量的标志:
NGX_HTTP_VAR_CHANGEABLE:允许重复定义;
NGX_HTTP_VAR_NOCACHEABLE:变量值不可以被缓存,每次使用必须计算;
NGX_HTTP_VAR_INDEXED:指示变量值存储在数组中,当使用ngx_http_get_variable函数获取变量时不会每次都为变量分配值空间;
NGX_HTTP_VAR_NOHASH:配置解析完以后,变量名不进hash索引,处理请求时不可以用变量名访问变量。
创建了变量以后,需要设置变量的get_handler和set_handler,以及data。比如map是这样设置的:
var-&get_handler = ngx_http_map_
var-&data = (uintptr_t)
变量的get_handler和set_handler体现了两种使用策略,
get_handler体现的是lazy_handle的策略,只有使用到变量,才会计算变量值;
set_handler体现的是active_handle的策略,每执行一个请求,都会计算变量值。
同时设置get_handler和set_handler回调函数是没有意义的,必须根据变量的使用特点,确定使用其中某一种回调函数。一般来说,get_handler更通用一些。
而这里设置的data将会作为将来调用get_handler或者set_handler的参数。
现在回到map的实现,nginx是利用创建变量的。
那可能有读者可能会有疑问了。如果某个变量设置了NGX_HTTP_VAR_NOHASH的标志,不能使用变量名访问,那应该怎么访问呢?
答案在这里:ngx_http_get_variable_index
ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t
这个函数会把变量加入一个array并返回变量在array中的下标,这样模块保存变量的下标,并通过ngx_http_get_indexed_variable
ngx_http_variable_value_t
ngx_http_get_indexed_variable(ngx_http_request_t *r,
ngx_uint_t index);
直接得到变量值。
下面另外给出一个简单的例子——mod_gray,来说明这种用法:
static char *
ngx_http_set_gray_handler(ngx_conf_t *cf, ngx_command_t
*cmd, void *conf)
&ngx_str_t&&&&&&&&&&&&&&&&&&&&
&ngx_http_variable_t&&&&&&&&&
&ngx_http_core_loc_conf_t&&&&
&ngx_http_gray_loc_conf_t&&&&
ngx_http_conf_get_module_loc_conf(cf,
ngx_http_core_module);
&clcf-&handler
= ngx_http_gray_
&ngx_str_set(&uri_var_name,
"gray_uri_args");
ngx_http_add_variable(cf, &uri_var_name,
NGX_HTTP_VAR_CHANGEABLE
&&&&&&&&&&&&&&&&&&&&&&&&&&&&|
NGX_HTTP_VAR_INDEXED);
&&& if (var == NULL)
return NGX_CONF_ERROR;
&glcf-&uri_var_index
= ngx_http_get_variable_index(cf,
&uri_var_name);
(glcf-&uri_var_index == NGX_ERROR) {
return NGX_CONF_ERROR;
var-&get_handler =
ngx_http_gray_var_
return NGX_CONF_OK;
变量回调函数
回调函数的原型是
ngx_http_variable_callback
(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data);
回调函数的工作是利用r和data的数据,设置变量值数据结构v的状态和属性。
其中状态有
no_cacheable
是否可以缓存
无法确定值
值字符串长度
值字符串起始地址
下面继续上面mod_gray的例子来说明回调函数的写法:
static ngx_int_t
ngx_http_gray_var_handler(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
v-&len = r-&uri.len +
v-&len += (r-&args.len
& 0 ? 1 : 0);
v-&data = ngx_palloc(r-&pool,
if (v-&data == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_memcpy(v-&data, r-&uri.data,
r-&uri.len);
if (r-&args.len) {
*(v-&data + r-&uri.len) =
ngx_memcpy(v-&data + r-&uri.len + 1,
r-&args.data,
r-&args.len);
v-&valid = 1;
v-&no_cacheable = 0;
return NGX_OK;
上面这种写法简单而且直接,而map模块的回调函数使用的是另一种方法——它在postconfiguration回调中生成所有可能的变量值,并将其存入一张表,就是那个map,那么在回调函数里面,只需要取得第一个变量的值,然后查表得到某个变量值返回就完成了,具体代码大家就自己去看一下吧。
直接引用变量
直接引用变量有3个API可以调用:
ngx_http_get_variable
ngx_http_get_flushed_variable
ngx_http_get_indexed_variable
ngx_http_variable_value_t
ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t
*name, ngx_uint_t key)
此API通过变量名和其hash得到变量值。如果变量标志含有NGX_HTTP_VAR_NOHASH,则无法通过此API得到变量值。如果变量标志含有NGX_HTTP_VAR_INDEXED,该API查找到此变量后,会尝试使用变量下标获取变量值,否则直接分配空间并计算变量值。
ngx_http_variable_value_t
ngx_http_get_flushed_variable(ngx_http_request_t *r,
ngx_uint_t index);
此API通过变量序号得到变量值。在变量标志含有NGX_HTTP_VAR_NOCACHEABLE时,它同时负责将变量值刷成无效。
ngx_http_variable_value_t
ngx_http_get_indexed_variable(ngx_http_request_t *r,
ngx_uint_t index);
此API通过变量序号得到变量值,但在变量标志含有NGX_HTTP_VAR_NOCACHEABLE时,它并不负责将变量值刷成无效,也不判断变量值是否应该无效。
继续mod_gray的例子:
static ngx_int_t
ngx_http_gray_handler(ngx_http_request_t *r)
ngx_http_gray_loc_conf_t&&&&
ngx_http_variable_value_t&&&
conf = ngx_http_get_module_loc_conf(r,
ngx_http_gray_module);
vv = ngx_http_get_indexed_variable(r,
conf-&uri_var_index);
if (vv == NULL || vv-&not_found) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
return NGX_OK;
map中没有直接引用变量的例子,因为map本身是构造一个变量给其他模块使用的。那么有的读者会问了,map中读取第一个参数不算么?这正是我们下面要介绍的,在参数值中引用变量。
参数值中引用变量
前面已经提到,map的第一个参数是在参数值中引用变量,而不是直接引用变量,那么这两者直接有什么不同呢?我们拿另外一个命令limit_req_zone来做个对比:
limit_req_zone&
$binary_remote_addr&
zone=one:10m&&
rate=1r/s;
limit_req_zone是直接引用变量的范例,其第一个参数必须是一个变量。而map可以这样来用:
$remote_addr-$request_uri $target { … }
map允许使用其第一个参数使用变量的组合,可以包含变量和其他字符串。这个特性看起来挺先进的,那怎么才能做到呢?还记得我们在《Nginx模块开发(二)》中见到的“footer”指令的参数么?对,那也是一个例子。
要实现这种参数值中引用变量,我们需要借助ngx_http_compile_complex_value_t。
我们再来看一下map。map实现参数值中引用变量是两步走,首先在配置解析阶段:
static char *
ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void
ngx_http_compile_complex_value_t&&
ngx_memzero(&ccv,
sizeof(ngx_http_compile_complex_value_t));
ccv.value = &value[1];
plex_value =
if (ngx_http_compile_complex_value(&ccv) != NGX_OK)
return NGX_CONF_ERROR;
这段和中的例子一样,都是从ngx_http_map_block中摘取出来的。这段代码利用ngx_http_compile_complex_value函数,将value[1]变量进行逆波兰变换,然后存入map-&value,等待后续出来。逆波兰变换是计算机语言中解析表达式最常用的方法,关于逆波兰变换的原理,我们在下一章再进行介绍。
好,我们继续。当nginx在处理一个具体的请求时,我们可以看到,map的创建的变量的get_handler,也就是ngx_http_map_variable中,含有如下代码:
ngx_http_map_ctx_t& *map =
(ngx_http_map_ctx_t *)
ngx_str_t&&&&&&&&&&&&&&&&&&
(ngx_http_complex_value(r,
&map-&value, &val)
!= NGX_OK) {
return NGX_ERROR;
这段代码将之前存储在map-&value中的编译表达式取出,使用ngx_http_complex_value计算表达式的,再将计算结果存入字符串变量val中。那么后续代码就可以从val得到当前的实际值。后面的事情就很好理解了。
这一章给大家展示了一下nginx变量的用法,包括配置和代码两个层面。在配置层面,介绍了创建变量和使用变量;在代码层面,介绍了创建变量、写变量回调函数、直接引用变量和在变量值中引用变量。相信读者对变量都有了基本认识。变通一点讲,代码中创建的变量配置中可用,配置中创建的变量代码中也可用。这点本文没有涉及,但是各位读者应该自己能想明白其中的道理。也正是因为这一点,nginx中的变量才是灵活的,可用的。
好啦,大家好好过个年吧!
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。SPSS Learning Module: Using SPSS functions for making and recoding variables
SPSS Learning Module
Using SPSS functions for making and recoding variables
1. Introduction
SPSS has a wide variety of functions you can use
for creating and recoding variables.& We will explore three kinds of
functions:
mathematical functions, string functions, and random number functions. These functions
have the same general syntax:
function_name(argument1, argument2, etc.)
We will illustrate some functions using the following data file that
includes name, x, test1, test2,
and test3.
DATA LIST FREE /
name (A14)
x test1 test2 test3.
BEGIN DATA.
&John Smith&
4.2 86.5 84.55
&Samuel Adams&
-99 82.37 -99
&Ben Johnson&
-6.2 82.1 84.81
&Chris Adraktas&
&John Brown&
-999 79.7 79.07
The output of the LIST command is shown
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
The variable x uses -999 to indicate
missing values, and test1, test2 and test3
use -99 to indicate missing values.& Below we tell SPSS about these missing values
and list out the data again.
MISSING VALUES x (-999)
/test1 test2 test3 (-99).
The output is shown below.& Note that the data
really does not look any different after we have defined the missing values.& But, as
we will see below, SPSS does know to treat these values as missing rather than treating
them as though they were -99 and -999.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
2. Math functions
Now let's try some basic math functions. The trunc
function (short for truncate) takes a number and converts it to a whole number (integer)
by removing all the decimal places, for example, 6.99 and 6.49 would become 6.& By
contrast, the rnd function (short for round) rounds numbers to the
nearest whole number using conventional rounding rules, for example 6.99 would become 7,
but 6.49 would become 6.&
COMPUTE t1tr = TRUNC(test1).
COMPUTE t2tr = TRUNC(test2).
COMPUTE t1rnd = RND(test1).
COMPUTE t2rnd = RND(test2).
LIST name test1 t1tr t1rnd test2 t2tr t2rnd.
The results below are as we would expect.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
SPSS has other mathematical functions.& Below we
illustrate functions for getting the square root (sqrt), natural log (ln),
log to the base 10 (lg10) and exponential (exp).&
Note that the sqrt, ln and lg10
functions do not work with negative numbers (for example you cannot take the square root
of a negative number).& SPSS will generate missing values in such cases, as we will
see below.
COMPUTE xsqrt = SQRT(x).
COMPUTE xln
COMPUTE xlg10 = LG10(x).
COMPUTE xexp
LIST x xsqrt xln xlg10 xexp.
The results are shown below.
expected SPSS to generate missing values for xsqrt, xln
and xlg10 when x was negative and we see below that
those values are displayed as a single decimal point.& This is the way that SPSS
shows a system missing value. Also, we see that xsqrt, xln, xlg10 and
xexp& were all assigned system missing
values when x was -999.
.98 13359.73
The results also included warnings like the one shown
below.& The one below is telling us that you cannot take the square root of a
negative number and that SPSS is going to set the result to the system missing value.
Warning # 603
The argument to the square root function is less than zero.
The result has
been set to the system-missing value.
3. Statistical functions
SPSS also has statistical functions that operate on one
or more variables.& For example, we might want to compute the average of the three
test scores.& SPSS has the MEAN function that can do that for you,
as shown below.
COMPUTE avg = MEAN(test1, test2, test3).
LIST name test1 test2 test3 avg.
We see the results below.& Note that SPSS computed
the mean of the non missing values.& For Samuel Adams, that meant that his average
was the same as his score on test2 since that was the only non-missing
value.& We could tell SPSS to give anyone a missing value if they have fewer than 2
valid test scores using the mean.2 function.& Likewise, we could
tell SPSS that we want the mean to be missing if any of the scores were missing, by using
the mean.3 function.& These are illustrated below.
COMPUTE avg2 = MEAN.2(test1, test2, test3).
COMPUTE avg3 = MEAN.3(test1, test2, test3).
LIST name test1 test2 test3 avg avg2 avg3.
As you see below, avg2 is missing for
Samuel Adams, and avg3 is also missing for Samuel Adams and Chris
Adraktas because they both had some missing test scores.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
In addition to the mean function, SPSS
also has sum, sd, variance, min and max functions.
4. String functions
Now let's illustrate some of the SPSS string functions.&
Below we create up that will be the name converted into upper
case, lo that will be the name converted to lower case, and sub that will
be the third through eighth character in the persons name.& Note that we first had to use
the string command to tell SPSS that up lo
and sub are string variables that will have a length of up to 14
characters.& Had we omitted the string command, these would have
been treated as numeric variables, and when SPSS tried to assign a character value to the
numeric variables, it would have generated an error.& We also create len
that is the length of the name variable, and len2 that is the length of
the persons name.
STRING up lo (A14)
/sub (A6).
COMPUTE up
= UPCASE(name).
COMPUTE lo
= LOWER(name).
COMPUTE sub
= SUBSTR(name,3,8).
COMPUTE len = LENGTH(name).
COMPUTE len2 = LENGTH(RTRIM(name)).
LIST name up lo sub len len2.
The results are shown below.& The results for up lo sub all as we would expect.& The result for
may be a bit confusing.& The variable len does not refer to the
length of the person's name, but it refers to the length of the variable name.
& When we read the data we entered
name (A14) for name, giving the variable a length of 14, and that is why
is always 14.& By contrast, len2 uses the rtrim
function to strip off any excess blanks, and then it takes the length of that.& In
the end, len2 returns the length of the persons name, for example John
Smith has a length of 10.
John Smith
JOHN SMITH
john smith
Samuel Adams
SAMUEL ADAMS
samuel adams
Ben Johnson
BEN JOHNSON
ben johnson
Chris Adraktas CHRIS ADRAKTAS chris adraktas ris Ad
John Brown
JOHN BROWN
john brown
Let's use SPSS string functions to get the first name
and last name out of the name variable.& We start by using the
function to determine the position of the first blank space in the name.& We then use
the substr function to extract the part of the name before the blank to
be the first name, and the part after the blank to be the last name.
STRING fname lname (A10).
COMPUTE blank = INDEX(name,' ').
COMPUTE fname = SUBSTR(name,1,blank-1).
COMPUTE lname = SUBSTR(name,blank+1).
LIST name blank fname lname.
The results below show that this was successful.&
For example, for John Smith, the substr function extracted the first name
by taking the substring from the 1st to 4th character of name, and the
last name by taking the 6th character and onward.
BLANK FNAME
John Smith
Samuel Adams
7.00 Samuel
Ben Johnson
Chris Adraktas
6.00 Chris
John Brown
5. Random number functions
Random numbers are more useful than you might
imagine, they are used extensively in Monte Carlo studies, but they are also frequently
used in many other situation We will look at two of SPSS's random number functions
uniform(n) - generates a random number that is 0 or
greater, and less than n from a uniform distribution.
rv.binomial(n,p) - generates a value from the binomial
distribution with n trials, and with a probability of success equal to p.
Below we generate a random number that is greater
than or equal to 0, but less than 1.
COMPUTE rannum = UNIFORM(1).
LIST name rannum.
We see the results below.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
Below we generate a random number that is greater than
or equal to 0, but less than 10.
COMPUTE ran10 = UNIFORM(10).
LIST NAME ran10.
And the results are shown below.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
The example below generates a whole number (integer)
from 1 to 100.& The trucn function is used to convert the result
into a whole number from 0 to 99, and then 1 is added to make it from 1 to 100.
COMPUTE ran100 = TRUNC(UNIFORM(100)) + 1.
LIST name ran100.
As we see below, these values are all whole numbers.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
Below we use the rv.binomial function
to simulate a coin flip.& It is like a coin flip since the number of trials is 1 and
the probability of success is .5 (like flipping a coin once and the probability of it
coming up heads is .5).& Let's treat a 1 as coming up heads, and a 0 as coming up tails.& As we see below, Ben and John each got a head, and the others got tails.
COMPUTE flip = RV.BINOMIAL(1 , .5 ).
LIST name flip.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
Below, we change the number of flips to 10, and count
the number of heads each person gets.& John got the most heads (7) and Ben got the fewest
COMPUTE flip10 = RV.BINOMIAL(10 , .5 ).
LIST name flip10.
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
The next example changes the flips to 100.& It also
sets the seed for the random number generator.& The seed
determines the string of random numbers that will be generated.& John got the fewest
heads (49 out of 100) and Samuel got the most (58 out of 100).
SET SEED = 149238.
COMPUTE flip100 = RV.BINOMIAL(100 , .5 ).
LIST name flip100 .
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
If we repeat the example from above using the exact same
seed, we will get the same results.& This is very useful for being
able to replicate results of a simulation study or Monte Carlo style study.& Indeed,
using the same seed did generate the same results (see below).
SET SEED = 149238.
COMPUTE flip100 = RV.BINOMIAL(100 , .5 ).
LIST name flip100 .
John Smith
Samuel Adams
Ben Johnson
Chris Adraktas
John Brown
6. Random number functions, advanced
In the examples above, we used the rv.binomial
function to simulate coin flips but it gave us the end result of all of the flips. &
Perhaps you would like to do a simulation study where you generate each of the flips as a
separate observation.& SPSS can do this, as we illustrate below.
SET seed=943785.
INPUT PROGRAM.
+ LOOP id = 1 to 25.
COMPUTE cointoss = RV.BINOMIAL( 1 , .5 ).
+ END LOOP.
+ END FILE.
END INPUT PROGRAM.
LIST CASES.
The program above creates 25 observations, each having a
variable called id which is the trial number, and cointoss
that will be either 1 or 0.& Even if this program does not make much sense to you,
you could use it as a template to make your own simulation.& You can change the
number of trials by changing 25 to the number of trials you want.& You can change the
probability of success by changing the value of .5 to the value you would like.& Or,
you could choose an entirely different random number generating function instead of
rv.binomial
you might choose uniform.& The results of the program above are
shown below.
ID COINTOSS
3. Problems to look out for
Watch out for math errors, such as division by zero,
square root of a negative number and log of a negative number.
4. For more information
For information on Functions is SPSS consult the SPSS
Command Syntax Reference Guide.
The content of this web site should not be construed as an endorsement
of any particular web site, book, or software product by the
University of California.}

我要回帖

更多关于 锂电池电动车 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信