统一随机数从0到1 in c

时间:2017-05-16 19:26:50

标签: c random

我想对排队模型进行模拟。所以,当我的随机数是< =从阈值开始时,我想我有一个到达,或者如果它是>我想我有一个离开。问题是,当我使用时:

srand((unsigned)time(NULL));
random_num= ((double) rand() / (RAND_MAX));
printf("Random number is :%lf\n",random_num);

我看到它打印相同的数字很多次然后它改变它。逻辑上问题是关于种子,我能做些什么来解决这个问题?我想要很多0到1之间的统一随机数,大约150000个随机数。 谢谢你的推荐 编辑:这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
        int count=0,state=0,MAXIMUM=200000,i=0,k,f=0;   /* initialize values*/
        double p[10]={0},random_num,threshold,arrivals=0.0,arrivals_ar[10]={0.0},average,average_prev=0,diff,m_a=2.0,m_b=2.0,l;/* initialize*/
        printf("Give the value of lamda,choose between {1,2,3}\n");
        scanf("%lf",&l);                    /* as for lamda value*/
        if((l==1.0) || (l==2.0) || (l==3.0))
                        f=1;
        while(f==0){                    /* If value is wrong,ask again*/
            printf("You gave wrong value.Try again a number between {1,2,3}\n");
            scanf("%lf",&l);
            if((l==1.0) || (l==2.0) || (l==3.0))
                f=1;
         }
        threshold=l/(l+m_a);
        printf("Give the value of k,choose between {1,2,3,4,5,6,7,8,9}\n");
        f=0;
        scanf("%d",&k);
        if((k>=1) && (k<=9))
            f=1;
        while(f==0){
            printf("You gave wrong value,try again.Choose a number between {1,2,3,4,5,6,7,8,9}\n");
            scanf("%d",&k);
                if((k>=1) && (k<=9))
                        f=1;}

        printf("you gave lamda= %lf \n",l);
            printf("You gave kapa=%d\n",k);
ARRIVAL:    arrivals=arrivals+1;
        arrivals_ar[state]=arrivals_ar[state]+1;
        count=count+1;
        if (state==10){
            goto LOOP;}
        else{
            state=state+1;}
        if(state>k){
            printf("Now i use both servers\n");
                        threshold=l/(l+m_a+m_b);}
LOOP:       if (state==0){
            goto ARRIVAL;}
        else{
            srand((unsigned)time(NULL));
            random_num= ((double) rand() / (RAND_MAX));
            printf("Random number is :%e\n",random_num);
            if(random_num<threshold){
                goto ARRIVAL;}
            else{
                goto DEPARTURE;}}   

DEPARTURE:  count=count+1;
        state=state-1;
        if(state<=k){
            printf("Now i use only one server\n");
                        threshold=l/(l+m_a);}
        if(count<MAXIMUM){
            goto LOOP;}
        else{
            printf("Count has gone to maximum\n");
            while(i<=10){
                p[i]=arrivals_ar[i]/arrivals;
                average=average+p[i]*i;
                i=i+1;              }}
        printf("Average is %lf\n",average);
}

1 个答案:

答案 0 :(得分:1)

srand将重新编号数字生成器。因为format(as.Date(sprintf('%d-01-01', 1999:2010)), '%y') 将以秒为单位给出时间差异,所以很可能在大多数循环周期中时间没有改变。这意味着每次您使用相同的数字重新播种随机生成器。这就是阻止数量变化的原因。

相反,你应该在循环之前调用Sub readWrite() Dim fileNum As Integer, dataLine As String, arrayCounter As Long, mySplitArray, tempSplit Dim myfiles, fileName As String Dim i As Integer, myArray() arrayCounter = 0 ReDim myArray(1 To 1) myfiles = Application.GetOpenFilename(filefilter:="TXT Files (*.txt), *.txt", MultiSelect:=True) If Not IsEmpty(myfiles) Then For i = LBound(myfiles) To UBound(myfiles) fileName = myfiles(i) tempSplit = Split(fileName, "\") fileName = tempSplit(UBound(tempSplit)) fileNum = FreeFile() Open myfiles(i) For Input As #fileNum While Not EOF(fileNum) Line Input #fileNum, dataLine If left(dataLine, 1) <> "#" Then arrayCounter = arrayCounter + 1 ReDim Preserve myArray(1 To arrayCounter) myArray(arrayCounter) = fileName & " " & dataLine End If Wend For k = LBound(myArray) To UBound(myArray) myString = "" mySplitArray = Split(myArray(k), " ") For j = LBound(mySplitArray) To UBound(mySplitArray) If mySplitArray(j) <> "" Then If j = 0 Then myString = myString & mySplitArray(j) & " " & mySplitArray(j + 1) Else If j <> 1 And j <> 2 Then myString = myString & vbTab & mySplitArray(j) End If End If End If Next j Range("A" & k & ":H" & k) = Split(myString, vbTab) ' Debug.Print myString Next k Close fileNum Next i End If End Sub 一次,然后在循环内调用time()