0

I want to make a simulation for a queueing model.So when my random number is <=from a threshold i suppose i have an arrival,or if it is > i suppose i have a departure.The problem is that when i use:

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

I see that it prints the same number a lot of times and then it changes it.Logicaly the problem is about the seed,what i can do to solve this problem?I want a lot of Uniform Random Numbers between 0 To 1,about 150000 random numbers. Thanks in advace Edit:here is my code

#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);
}
Nikos
  • 17
  • 1
  • 10
  • 7
    Just don't call `srand` in a loop as you presumably do. – ForceBru May 16 '17 at 19:28
  • Show your *loop*. – Eugene Sh. May 16 '17 at 19:28
  • 2) `"%f"` will only show 1,000,000 different print outs of numbers in the range 0 to 1. Use `"%e"` to see more – chux - Reinstate Monica May 16 '17 at 19:31
  • Aside: in MSVC the limit `RAND_MAX` is only `32767` so you would have to build your own random number from *two* calls to `rand()` (by shifting and adding) to obtain 150000 different random numbers. AFAIK gcc has broader range. – Weather Vane May 16 '17 at 19:35
  • 1
    Oh.. I wish you weren't showing it actually...It's horrible. – Eugene Sh. May 16 '17 at 19:43
  • Aside from the `srand()` issue, "Uniform Random Numbers between 0 To 1" could use clarity. Between [0.5...1.0) there are `n` different `double`. Between `[0.0...0.5)` there are `thousands*n` different `double`. By "uniform", do you want to generate about `2*n` different potential `double` between `[0...1.0)` or something else? – chux - Reinstate Monica May 16 '17 at 19:43
  • @chux And there are *infinite* non-doubles there... – Eugene Sh. May 16 '17 at 19:44
  • @ Eugene Sh i want to do exactly what you said.The code is so bad because i have to write since the first year,it is just a simulation i want to do – Nikos May 16 '17 at 19:47
  • [double rand_01(void)](http://stackoverflow.com/a/35927984/24103590) may work for you. – chux - Reinstate Monica May 16 '17 at 19:54
  • See [this answer](http://stackoverflow.com/a/6852396/134554) – John Bode May 16 '17 at 19:55
  • @chux i want to do what you said,do you have any solution?thanks in advance – Nikos May 16 '17 at 19:55
  • 2
    There are a select few reasons to use `goto` in C. Looping is **not** one of them. – dbush May 16 '17 at 19:59
  • @dbush1 for the use of the program i don't see any other way to make what i want – Nikos May 16 '17 at 20:02
  • "From 0 To 1": does that mean once in a while 0.0 is generated? once in a while 1.0 is generated? If code wants _both_ 0.0 and 1.0, then that adds extra trickier code to make _uniform_ results. Typically applications need [0.0 ...1.0), so values 0 to just below 1.0. What do you need.? – chux - Reinstate Monica May 16 '17 at 20:09
  • What are "uniform random numbers"? If you mean "uniformly distributed" then perhaps build a uniform array first, then randomly shuffle it. – Weather Vane May 16 '17 at 20:10
  • I just removed the srand((unsigned)time(NULL)); command from the place i have put it and move it at the begining of programm.Now every time i execute it produce diffrent number with not many diffrence at the average – Nikos May 16 '17 at 20:13
  • @WeatherVane - A uniform distribution doesn't have much to do with shuffling, AFAIK. – Oliver Charlesworth May 16 '17 at 20:27
  • @OliverCharlesworth but the "random" part might. The question is not asking about the worth of the PRNG. It is unclear. That is why I asked what are "uniform random numbers". – Weather Vane May 16 '17 at 20:36
  • @OliverCharlesworth random numbers are not "uniformly distributed". They are random. – Weather Vane May 16 '17 at 20:41
  • @WeatherVane - Indeed - that's precisely one has to specify "uniform(ly distributed)" in order to qualify what kind of random numbers are being referred to :) – Oliver Charlesworth May 16 '17 at 20:52
  • @ Weather Vane how can i do that? – Nikos May 16 '17 at 21:40

1 Answers1

1

srand is going to reseed the number generator. Because time() is going to give you the epoch difference in seconds, it's very likely that the time has not changed for the majority of loops cycles. Which means each time you're reseeding your random generator wit the same number. This is what's preventing the number from changing.

Rather, you should call srand(time((NULL)) once before the loop, and then call rand() inside the loop.

isick
  • 1,467
  • 1
  • 12
  • 23
  • Ι think that if i remove srand command it would also help – Nikos May 16 '17 at 20:01
  • @NikosTzagarakis remove `srand()` while proving and debugging, so you get repeatable results. Then place it at the start of `main`. – Weather Vane May 16 '17 at 20:12
  • I made that,but the problem is that if i run my simulation many times i get little different results.That's maybe because random numbers between 0 and 1 are not niformly distributed – Nikos May 16 '17 at 21:36