24小时热门版块排行榜    

查看: 1869  |  回复: 13

huanyingst

新虫 (初入文坛)

引用回帖:
3楼: Originally posted by 欢乐妞 at 2013-12-09 22:32:53
恰好我也用到随机数,这是我用c++写的,
头文件要包含
#include<cstdlib>
#include<ctime>
在c语言中包含的头文件去掉c就可以了,即
#include<stdlib.h>
#include<time.h>
srand((u ...

你好 请问晶包里有规律的替换原子用c怎么写啊~谢谢~
11楼2014-07-10 11:20:17
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

探索357

木虫 (正式写手)

C++里面随机数的产生于种子值有关,默认的话,种子是不变的,也就是每次产生的一系列随机数都相同,需要更改种子的值
12楼2014-07-12 19:59:38
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

weakood

新虫 (初入文坛)

【答案】应助回帖

#include <stdio.h>
#include <time.h>
#include <stdio.h>

int main(){
  int size = 100;   //可以自己设置要要到的范围
  int arr[size] = {};   
  printf("请选择是否要得到随机数,输入1表示要得到,输入0表示结束\n";
  int i, num;
  scanf("%d", &i);
  while(1 == i){
     num = rand()%100;
     if(!arr[num]){
         printf("你得到的随机数是:%d\n", num + 1);
         scanf("%d", &i);
     }else
          i = 1;
  }
  return 0;
}
13楼2014-07-27 16:57:58
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

枪下游魂

木虫 (著名写手)

【答案】应助回帖

#define M1 259200
#define IA1 7141
#define IC1 54773
#define RM1 (1.0/M1)
#define M2 134456
#define IA2 8121
#define IC2 28411
#define RM2 (1.0/M2)
#define M3 243000
#define IA3 4561
#define IC3 51349

double ran1(idum)
int *idum;
{
        static long ix1,ix2,ix3;
        static double r[98];
        double temp;
        static int iff=0;
        int j;
        void nrerror();

        if (*idum < 0 || iff == 0) {
                iff=1;
                ix1=(IC1-(*idum)) % M1;
                ix1=(IA1*ix1+IC1) % M1;
                ix2=ix1 % M2;
                ix1=(IA1*ix1+IC1) % M1;
                ix3=ix1 % M3;
                for (j=1;j<=97;j++) {
                        ix1=(IA1*ix1+IC1) % M1;
                        ix2=(IA2*ix2+IC2) % M2;
                        r[j]=(ix1+ix2*RM2)*RM1;
                }
                *idum=1;
        }
        ix1=(IA1*ix1+IC1) % M1;
        ix2=(IA2*ix2+IC2) % M2;
        ix3=(IA3*ix3+IC3) % M3;
        j=1 + ((97*ix3)/M3);
        if (j > 97 || j < 1) nrerror("RAN1: This cannot happen.";
        temp=r[j];
        r[j]=(ix1+ix2*RM2)*RM1;
        return temp;
}

#undef M1
#undef IA1
#undef IC1
#undef RM1
#undef M2
#undef IA2
#undef IC2
#undef RM2
#undef M3
#undef IA3
#undef IC3

以上 程序段名为“ran1.c”,产生0到1之间的随机数,出自 《numerical receipt in c》一书
相对于以时间来生成随机数,这个的连续性和随机性我觉得更好。
缺点为如果你给定一个初始种子,那每次产生的随机数相同。
14楼2014-07-28 09:11:04
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 elevenleo 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 科研孤儿太难了 +21 我4大白菜 2026-08-20 22/1100 2026-08-26 11:29 by nlgza
[基金申请] 系统查不到 +5 董八千 2026-08-26 5/250 2026-08-26 11:29 by panjy.cn
[基金申请] 怎么查啊 +4 huang1991js 2026-08-26 4/200 2026-08-26 11:26 by panjy.cn
[基金申请] 国合里面能看到了 +7 一怀馨秋 2026-08-26 7/350 2026-08-26 11:23 by zhaosm1982
[基金申请] 国际合作可查了,中了面上 +18 Ldrop2023 2026-08-26 18/900 2026-08-26 11:15 by cmrandy
[基金申请] 系统进不去 +4 yanglien 2026-08-26 5/250 2026-08-26 11:10 by wenfengw83
[基金申请] 2026年的国家社科基金项目通讯评审的新规则与新动向、新挑战 +6 process2012 2026-08-23 8/400 2026-08-26 11:03 by zjlp
[基金申请] 项目信息和经费信息在系统里都可以看到了 +6 wittyboy 2026-08-26 14/700 2026-08-26 10:55 by wittyboy
[基金申请] 今天务委会开完了,明天出结果吗 +19 angus9576 2026-08-25 23/1150 2026-08-26 10:03 by zp519
[基金申请] 国合现在查不到了吗? +10 chengyan1220 2026-08-24 20/1000 2026-08-26 08:57 by peasantsprig
[基金申请] 牛来!米来!面来! +8 beefly 2026-08-26 8/400 2026-08-26 08:37 by xuzhipiao
[基金申请] 2026国自然函评费到账 +21 羊腰板 2026-08-21 24/1200 2026-08-26 07:20 by finnigan
[基金申请] 某些机构,以效率低为荣,以效率低作为存在感 +9 yuleib84 2026-08-25 10/500 2026-08-25 17:14 by alexon
[基金申请] 如果此刻你正在为国基感到焦虑,不妨来听听这首《基金之外》 +8 scalable 2026-08-24 8/400 2026-08-25 12:52 by jnhyjjm
[基金申请] 人气不行了 +11 fansofjerry 2026-08-21 11/550 2026-08-25 11:04 by 孤独的英雄6
[基金申请] 建议基金发布提前给出明确的时间点 +13 kulium 2026-08-21 16/800 2026-08-24 16:27 by superceng
[基金申请] 让我中一个面上吧! +13 大萍1987 2026-08-20 16/800 2026-08-24 10:23 by 太傻了
[教师之家] 跳槽后在研项目怎么办? +5 简单化xn 2026-08-22 10/500 2026-08-23 12:38 by 简单化xn
[基金申请] 时间戳今天,20号变了 +5 archvillain 2026-08-20 5/250 2026-08-22 06:12 by hui_daxiao
[基金申请] 基金啊基金 +4 longfie172 2026-08-20 4/200 2026-08-21 08:58 by mark mao
信息提示
请填处理意见