Thread: Help with this code please!

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    16

    Help with this code please!

    First of all, sorry about the language of arguments and functions(they are on spanish ). The problem is the following: I need to this function to get a random number between the limits 1 and the number inserted by the user, but I just get a random number that is not between these limits, here is the code.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<malloc.h>
    #include<time.h>
    
    
    struct Bombo{
           int n;
           int *v;
    };
    
    
    struct Bombo BomboCrea(int n){
          int i = 1;
          struct Bombo a;
          a.n = n;
          a.v = (int *)malloc(sizeof(int)*a.n);
          for(i; i<n; i++){
                 a.v[i] = 1+(rand()%(n-1));
                 printf("%d,",a.v[i]);
    }
    return a;
    }
    
    
    void BomboLibera(struct Bombo b){
         free(b.v);
    }
    
    
    int BomboExtrae(struct Bombo b){
        int a;
        int d;
        a = b.n;
        if(b.v == NULL){
             printf("El Bombo esta vacio");
             }else{
                  d = (1+(rand()%(a-1)));
                  };
    return d;
    }
    
    
    int BomboVacio(struct Bombo b){
        int a;
        if(b.v == NULL){
             a = 1;
             }else{
                   a = 0;
                   }
    return a;
    }
    
    
    int main(){
    srand(time(NULL));
    struct Bombo a;
    int b;
    int c;
    int d;
    int i = 0;
    printf("Escribe el tamaño del bombo desde 1 a ... \n");
    scanf("%d",&b);
    BomboCrea(b);
    c = BomboExtrae(a);
    printf("El numero es %d \n",c);
    BomboLibera(a);
    d = BomboVacio(a);
    system("pause");
    return 0;
    }
    the function BomboCrea works fine, I checked it and this make an array between the 2 limits
    Last edited by zetaXX; 07-23-2013 at 06:14 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Check the example here to get a random number between min and max
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by std10093 View Post
    Check the example here to get a random number between min and max
    Still not working

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What is the problem?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by std10093 View Post
    What is the problem?
    NVM Im stupid I was not assigning the function BomboCrea to a variable, thx anyways ^^

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You mean you were able to solve your problem 100% by your own. Well, I do not know if in Spain, they call this person a stupid one, but where I have traveled so far, they do not call it like that

    EDIT: Been to Barcelona, Spain :P
    Last edited by std10093; 07-23-2013 at 07:06 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zetaXX View Post
    Still not working
    Study this, and see if it helps.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void getRandom(int *randomNumber);
    
    int main(void) {
       int randomNumber=0, i;
       const int max=120;
    
       srand(time(NULL));
    
       for(i=0;i<max;i++) {
          getRandom(&randomNumber);
          printf("%2d\t",randomNumber);
       }
       printf("\n");
       return 0;
    }
    void getRandom(int *randomNumber) {
       int low=1, high=100; 
       *randomNumber = rand() % high + low;
    }
    I haven't studied this to see how random it is - my guess is, it's random enough for a fun program, but definitely not random enough to bet money on. Definitely!

    Edit: Late to the party!

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Adak:
    You define low in your getRandom function, but you only use it to adjust the base of your range, not the "width" of it as well. For example, If you defined low as 50, your code would produce values from 50-149, not 50-100, as might be expected

    @std10093:
    You need to reduce rand() modulo (max - min + 1). Otherwise, for a range of 0-10, you will only get values 0-9, never 10.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Thank you very much
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thank you. Haven't been using rand() for a long time now.

    So line 22 above should be:
    Code:
    *randomNumber = rand() % (high-low+1) +low ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM