| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1019 人关注过本帖
标题:求助 一道c++程序题的挑战
只看楼主 加入收藏
flyhack
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-24
收藏
 问题点数:0 回复次数:0 
求助 一道c++程序题的挑战
题目:(A)设计可生成均匀分布随机数据矩阵的C++类及相应的测试主程序。所生成的矩阵在该类对象退

出作用域时可自动存入磁盘文件,该磁盘文件名及其存储路径有默认值,也可通过类成员函数设置更改

;矩阵的行、列数有默认值,也可通过类成员函数设置更改;每一列数据为同一分布,不同列数据为不

同分布,均匀分布参数a、b有默认值,也可通过类成员函数设置更改。
提示:
程序代码:
(A)      磁盘文件名及其存储路径可分别用字符串类(string)实现。均匀分布随机数可用系统提

供的随机数函数生成(随机性能较差),也可按下列质数模乘线性共轭法生成(随机性能优良)(下列

为C-函数实现示例)


 

// lcgrand.cpp


 

/* Prime modulus multiplicative linear congruential generator--质数模乘线性共轭法产生器

   Z[i] = (630360016 * Z[i-1]) (mod(pow(2,31) - 1)), based on Marse and Roberts'

   portable FORTRAN random-number generator UNIRAN.  Multiple (100) streams are

   supported, with seeds spaced 100,000 apart.  Throughout, input argument

   "stream" must be an int giving the desired stream number.  


 

   Usage: (Three functions) — 用法


 

   1. To obtain the next U(0,1) random number from stream "stream," execute

          u = lcgrand(stream);

      where lcgrand is a float function.  The float variable u will contain the

      next random number.


 

   2. To set the seed for stream "stream" to a desired value zset, execute

          lcgrandst(zset, stream);

      where lcgrandst is a void function and zset must be a long set to the

      desired seed, a number between 1 and 2147483646 (inclusive).  Default

      seeds for all 100 streams are given in the code.


 

   3. To get the current (most recently used) integer in the sequence being

      generated for stream "stream" into the long variable zget, execute

          zget = lcgrandgt(stream);

      where lcgrandgt is a long function. */


 

/* Define the constants. */


 

#define MODLUS 2147483647

#define MULT1       24112

#define MULT2       26143


 

/* Set the default seeds for all 100 streams. */


 

static long zrng[] =

{         1,


 1973272912, 281629770,  20006270,1280689831,2096730329,1933576050,

  913566091, 246780520,1363774876, 604901985,1511192140,1259851944,

  824064364, 150493284, 242708531,  75253171,1964472944,1202299975,

  233217322,1911216000, 726370533, 403498145, 993232223,1103205531,

  762430696,1922803170,1385516923,  76271663, 413682397, 726466604,

  336157058,1432650381,1120463904, 595778810, 877722890,1046574445,

   68911991,2088367019, 748545416, 622401386,2122378830, 640690903,


 1774806513,2132545692,2079249579,  78130110, 852776735,1187867272,


 1351423507,1645973084,1997049139, 922510944,2045512870, 898585771,

  243649545,1004818771, 773686062, 403188473, 372279877,1901633463,

  498067494,2087759558, 493157915, 597104727,1530940798,1814496276,

  536444882,1663153658, 855503735,  67784357,1432404475, 619691088,

  119025595, 880802310, 176192644,1116780070, 277854671,1366580350,


 1142483975,2026948561,1053920743, 786262391,1792203830,1494667770,


 1923011392,1433700034,1244184613,1147297105, 539712780,1545929719,

  190641742,1645390429, 264907697, 620389253,1502074852, 927711160,

  364849192,2049576050, 638580085, 547070247 };


 

/* Generate the next random number. */


 

float lcgrand(int stream)

{

    long zi, lowprd, hi31;


 

    zi     = zrng[stream];

    lowprd = (zi & 65535) * MULT1;

    hi31   = (zi >> 16) * MULT1 + (lowprd >> 16);

    zi     = ((lowprd & 65535) - MODLUS) +

             ((hi31 & 32767) << 16) + (hi31 >> 15);

    if (zi < 0) zi += MODLUS;

    lowprd = (zi & 65535) * MULT2;

    hi31   = (zi >> 16) * MULT2 + (lowprd >> 16);

    zi     = ((lowprd & 65535) - MODLUS) +

             ((hi31 & 32767) << 16) + (hi31 >> 15);

    if (zi < 0) zi += MODLUS;

    zrng[stream] = zi;

    return (zi >> 7 | 1) / 16777216.0;

}


 


 


 

void lcgrandst (long zset, int stream) /* Set the current zrng for stream

                                          "stream" to zset. */

{

    zrng[stream] = zset;

}


 


 

long lcgrandgt (int stream) /* Return the current zrng for stream "stream". */

{

    return zrng[stream];

}


有哪位朋友帮忙看下,给出主要代码.谢谢!
搜索更多相关主题的帖子: 挑战 
2008-07-24 12:58
快速回复:求助 一道c++程序题的挑战
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015487 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved