| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖
标题:大家好,我想问一个随机函数然后进行判断的问题,谢谢!
只看楼主 加入收藏
rual1989
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
大家好,我想问一个随机函数然后进行判断的问题,谢谢!
大家好,我想问问能不能在指定的时间内得出一个随机数,例如说我想在一直进行每隔10秒产生一个10-100的随机数,然后对产生的这个数进行判断,我是新手,不怎么会写,请大家指教一下,谢谢
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#inclede <time.h>
/*线程一*/
void thread1(void)
{
int i=0;
int j;
srand((unsigned)time(NULL));
while(1){
for(i=0;i<10;i++)
{j=rand()%100;}
printf("%d\n",j);
}
}

/*线程二*/
void thread2(void)
{if(j>=40)
 printf("The num is over!"\n);
 else
 printf("OK!\n");
}
.....

上面是我自己写了一些,有点乱,老师也没讲过随机函数,其实我上面的程序表达的意思是其中一个线程在一直进行每隔10秒产生一个10-100的随机数,然后另一个线程对产生的这个数进行判断,大于40即打印警告,其他则输出OK。
有劳各位,谢谢
搜索更多相关主题的帖子: 时间 
2011-04-13 16:52
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:7 
用sleep函数来实现间隔,比如在gcc里
sleep(10); //10秒

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-04-13 17:57
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:7 
别忘了#include <windows.h>

                                         
===========深入<----------------->浅出============
2011-04-13 19:09
rual1989
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-13
收藏
得分:0 
谢谢,,,我是在linux下编程的,但是编译时找不到windows.h,
现在我的程序改成这样
/*线程一*/
void thread1(void)
{
 int i=0;
 int j;
 srand((unsigned)time(NULL));
 while(1){
 for(i=0;i<10;i++)
 {sleep(10);
 j=rand()%100;}
 printf("%d\n",j);}
}

/*线程二*/
void thread2(void)
{if(j>=40)
 printf("The num is over!\n");
 else
 printf("OK!\n");
}

编译中说线程二第一次用j,就是没有定义j吧,我想将线程一中的j值传给线程二,应该如何改写?
谢谢
2011-04-16 21:48
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:7 
问一下 这个东西与多线程有什么关系??

without further ado, let’s get started
2011-04-16 22:09
rual1989
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-13
收藏
得分:0 
我上面写了想要实现的意思,就是第一个线程给出一个随机数,然后将这个随机数传给第二个线程做判断,但是不会写传递那部分
2011-04-16 22:12
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
采用管道进行通信。

without further ado, let’s get started
2011-04-16 22:20
rual1989
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-13
收藏
得分:0 
我C语言才刚上手,我们同时又在学嵌入式linux,抱歉现在我也不知道什么是管道,能帮我写一下吗,我作为参考以后也当个例子,谢谢
2011-04-16 22:29
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
我直接用了一个全局变量模拟管道读写。
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10

pthread_t thread[2];
pthread_mutex_t mut;
int number=0 ;

void *thread1()
{
  int i=0;
  int j;
  srand((unsigned)time(NULL));
  while (1) {
       for (i=0;i<10;i++){
            j=rand()%100;
            sleep(3) ;
            number = j ;
       }
       number = -1 ;
  }
      pthread_exit(NULL);
}

void *thread2()
{
while (1) {
      if (number >= 40)
         printf("The num is over!\n");
      else
         printf("OK!\n");
      sleep(3) ;
}
 pthread_exit(NULL);
}

void thread_create(void)
{
        int temp;
        memset(&thread, 0, sizeof(thread));
        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)
                printf("create thread 1 error!\n");
        else
                printf("created thread 1\n");

        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
                printf("create thread2 error");
        else
                printf("created thread 2 \n");
}

void thread_wait(void)
{

        if(thread[0] !=0) {
                pthread_join(thread[0],NULL);
                printf("thread 1 finish \n");
        }
        if(thread[1] !=0) {
                pthread_join(thread[1],NULL);
                printf("thread 2 finish \n");
        }
}

int main()
{

        pthread_mutex_init(&mut,NULL);
             thread_create();
             thread_wait();
        return 0;
}

without further ado, let’s get started
2011-04-16 23:16
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
以下是引用rual1989在2011-4-16 22:29:56的发言:

我C语言才刚上手,我们同时又在学嵌入式linux,抱歉现在我也不知道什么是管道,能帮我写一下吗,我作为参考以后也当个例子,谢谢


你真强悍。

without further ado, let’s get started
2011-04-16 23:17
快速回复:大家好,我想问一个随机函数然后进行判断的问题,谢谢!
数据加载中...
 
   



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

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