Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Simple C question for you coding freaks!

  • 25-11-2004 7:21pm
    #1
    Closed Accounts Posts: 484 ✭✭


    lads i've just finished my first project of the year but i'm stuck on one thing.
    im coding in C, and i just need a random integer between 15 and 25 to be generated.

    i've tried:
    num1 = randomInteger(10) + 15;

    it should work but i'm getting an error on that line - unrsolved symbol blah blah

    would apreciate the help lads
    cheers


Comments

  • Registered Users, Registered Users 2 Posts: 2,758 ✭✭✭Peace


    post your code.....


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Here's code to generate A random number, but not within the range you want.

    Its up to you to figure out how thats done. Also, this is probably quite a complicated way of doing it, there are probably easier ways i don't know of :p
    #include <stdio.h>
    #include <stdlib.h>
    
    int randomInteger(int n)
    {
      return rand() % n;
    }
    
    int main()
    {
      int i = 0;
      while (i < 10)
        {
          printf("%d\n", randomInteger(600));
          i++;
        }
      return 0;
    }
    


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Why are you writting a function that's only functionality is to call another function???


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    jessy wrote:
    Why are you writting a function that's only functionality is to call another function???
    Because its good practise to keep individual functionality in individual methods?


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Because its good practise to keep individual functionality in individual methods?

    I dont understand your answer. what other functionality can rand() have? and if it is the case why is the printf() not been called from another function?


  • Advertisement
  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    // random number between 1 and 100
    randNumber = (rand() % 100) +1;


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Yeah, ok, fair point. In this case it was unnecessary... I didn't look at the code properly and thought there was more going on...


  • Registered Users, Registered Users 2 Posts: 1,038 ✭✭✭rob1891


    don't forget to seed your random number, otherwise it is not going to be very random!

    #include <stdlib.h>
    #include <time.h>

    ....

    srand(time(NULL));

    ....


    If you don't do this, your program will be using the same random number every time you run it ... :eek:


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    I study C and I didn't understand a word you guys typed!...I've got a problem with an assignment I might be back to yee before the end of the week...cheers lol


  • Registered Users, Registered Users 2 Posts: 95 ✭✭Mr.StRiPe


    #include <stdlib.h>
    
    
    int num1 = (rand() % 11) +15;
    
    //(rand() % 11) will return a random num between 0 - 10
    //Therefore adding 15 to it will give you a num between 15 - 25
    
    


  • Advertisement
  • Closed Accounts Posts: 484 ✭✭ManWithThePlan


    cheers lads i used a combination of a few answers and i got it!

    Cheers again!!


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    jessy wrote:
    Why are you writting a function that's only functionality is to call another function???
    Because i like things complicated. Plus, its the way i was shown how to do it before :p

    Anyway, glad to see you figured out a working answer.Tis why we're here!


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Because i like things complicated. Plus, its the way i was shown how to do it before :p

    Anyway, glad to see you figured out a working answer.Tis why we're here!

    Im all for High Coupling And Low Cohesion but thats a bit extream putting standard llibrary functions in other functions just to call them. still ya had the right answer(apart from seeding it):)


  • Registered Users, Registered Users 2 Posts: 1,391 ✭✭✭fatherdougalmag


    You obviously haven't had the joy making a Windows driver WHQL/HCT compliant. There's a nice money-spinner for the boys at Microsoft.

    There's a particular test called the Kernel Calls test. The idea is that depending on your driver, you're only allowed to call certain functions in the kernel. In the days of NT you pretty much used the ntoskrnl and co. for doing your stuff. Then for Windows 2000 and on they insisted you use, for example, NDIS.SYS (networking) for doing your stuff. Hook up your debugger and step into an NdisXXXX routine - hey presto - it just calls the fecking NTOSKRNL equiv which you previously called in your driver. But if you submit your driver ($$$$) and fail you've got to resubmit (more $$$).

    So there's you spending mundo dosh on a graphics adapter and during the driver installation a dialog pops up informing you that Microsoft haven't verified the driver. What do you do? Click cancel and feck the whole lot in the bin? I don't think so. That's the most hated dialog in our lives. The hours and hours of testing that need to go into just having that silly little dialog not pop up during your driver install. You should see some of the lengths some driver providers go to to auto-click it away. Madness.

    Microsfoft, Ted. Shower of bastards.


Advertisement