1 Replies - 185 Views - Last Post: 27 February 2018 - 01:59 PM Rate Topic: -----

#1 kulturman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-February 18

Need your opinions about a programming test

Posted 27 February 2018 - 08:10 AM

Hello everybody, this is my first message on DIC and as specified in title I need your opinions about a test. Codes are in scilab and C but I thought I could post here.

1) Write a scilab programm that that simulates exponential law(distribution?) of parameter lambda

function[Y] expo_low(lambda)
     rand('uniform')
     Y = -log(rand(1 , 1)) / lambda
endfunction

expo_low(0.5)



2.1) A scilab programm that genererates n random numbers that follow uniform distribution on [a , b]
function[] aleatoires(n , a , B)/>
    k = 1
    rand('seed' , getdate('s'))
    while(k <= n)
        rand(1 , 1) * (b - a) + a
        k = k + 1;
    end
endfunction

aleatoires(10 , 1 , 2)



2.2) The same programm in C

void aleatoires(int n , int a , int B)/>
{
    int i = 1;
    while(i <= n)
    {
        printf("%f" , (rand() / RAND_MAX) * (b - a) + a);
        i++;
    }
}


int main(void)
{
    srand(time(NULL));
    aleatoires(10 , 1 , 2);
    return 0;
}




Now I see that I forgot a few things(like not casting "time" result to double or float). I just want to know if the path is the right one. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Need your opinions about a programming test

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: Need your opinions about a programming test

Posted 27 February 2018 - 01:59 PM

In opinion, the C code would be more easily understood by people practiced in the art if you use the modulus operator, instead of multiplying the ratio with the range.

Also, in general, if the value to be printed out is an integer, you should use the format specifier for an integer (as opposed to the format specifier for a float that you are currently using).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1