0

*Im trying to make a dice program using C for my university but ive been specifically asked by the lecturer not to modify the main function he has given me but i have no clue how i can add math commands to his function

float getRand() {
    return rand() / (RAND_MAX+1.0);
}

to make this dice.

P.S. Im only learning so all of this is new

This is the task:

Write a "dice rolling" game. You are probably familiar with 6-sided dice, but some games use dice with 4, 6, 8, 10, 20, and 100 sides!

Make the computer pick a random number for a 6-sided die and a 20-sided die. Use the getRand() function, but do not modify it. Write an int rollDie(...) function to get a random value. have one integer argument for the value of the die (int number_of_sides), call the getRand() function. take the value it returns (a float between 0 and 0.999...) and do some math to transform that into an int between 1 and number_of_sides inclusive). return the number. Your int main() must contain only:

int main() {
    srand( time(NULL) ); // init random
    getRand(); // kick-start the random numbers

    int value = 0;
    value = rollDie(6);
    printf("6-sided die: %i\n", value);

    value = rollDie(20);
    printf("20-sided die: %i\n", value);

    getchar(); 
 }
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • Hint: you shouldn't modify `getRand()` either. Do something like `int rollDie(int max) { /* your code calling getRand() here */ }`. –  Oct 10 '15 at 18:47
  • And second remark: It's *insane* to use floats for this, but if you're requested to do so, well then do it ... just think what you would do to transform a number between 0 and 1 to a number between 1 and 6 -- really should be simple mathematics! –  Oct 10 '15 at 18:50
  • 1
    @naltipar: you shouldn't remove syntax errors or change bracing styles when editing questions... –  Oct 10 '15 at 18:52
  • The function getRand() was supposedly given to the OP by the professor. So I believe that the OP just made a mistake of not copying the entire function. – codingEnthusiast Oct 10 '15 at 18:54
  • Third remark, this line: `getRand(); // kick-start the random numbers` is totally pointless, just throwing away the first random number. `srand()` is enough! –  Oct 10 '15 at 18:54
  • @naltipar yes, and this professor seems kind of clueless ... –  Oct 10 '15 at 18:55
  • You have dice with fractional numbers? Whoever made that constraint is not walking, but brain D??d. – too honest for this site Oct 10 '15 at 18:55
  • 1
    @Olaf: `rollDie()` what OP is expected to implement should return `int`, so, ok so far. I guess the `float getRand()` might be here as an "easy way" to avoid bias introduced by the typical modulo ... strange enough. –  Oct 10 '15 at 19:02
  • @OP You need to do a little math to determine the equation of a line, given two points. The x-axis goes from 0 to just under 1 (that's what the `getRand` function gives you). The y values range from 1 to just under 7 (that's what your function is supposed to return), so the two points on the line are {0,1} and {1,7}. The rest is just math, and one line of code. – user3386109 Oct 10 '15 at 19:14
  • @FelixPalmen: I got that already. But the float itself introduces rouding errors, i.e. non-uniform distribution. There is actually no advantage, even less, as it uses `rand` internally. Typical academical (aka-dämlich) requirement. – too honest for this site Oct 10 '15 at 19:18
  • @Olaf why this "professor" not tried *at least* with `double` indeed goes beyond my mind ... and yes, a simple `rand() % n` would probably be better here. –  Oct 10 '15 at 19:19
  • 1
    @FelixPalmen: Neither is correct. You have to re-roll. There are many examples here on SO alone, not to mention the "rest" of the net. – too honest for this site Oct 10 '15 at 19:22
  • @Olaf of course, but at least `double` would be ... uhm .. "less evil" :) –  Oct 10 '15 at 19:24
  • @FelixPalmen: https://en.wikipedia.org/wiki/The_Lesser_of_Two_Evils ... It is s decission between cutting off your left or right leg. A prof should know better. This is just teaching to generate wrong code. There are better and more correct ways to teach whatever that should teach. – too honest for this site Oct 10 '15 at 19:28
  • @Olaf I don't see a huge problem with `rand()%n` in some game, given `RAND_MAX` is big and `n` is small. There will be a really tiny bias that just won't matter for a game. The problem I see here is a professor teaching weird methods of "solving" this. [I just happened to use two nice C# libs named bender and flexo for production code ... well, unrelated here :)] –  Oct 10 '15 at 19:30
  • @FelixPalmen: It is a question of doing it right. A student should be sensitised to understand the problems. Things may be different teaching small kids. The result of such faulty code can be found everywhere, from so called "professionals" with the result that you get , strange results lateron and when you have been hired to fix that, they tell you "but I learned it that way and it always worked". Try to find such errors! – too honest for this site Oct 10 '15 at 19:36
  • @Olaf let's agree you should *understand* such problems and a professor teaching this should take a proper approach (so this professor is a jerk). Actually *knowing* about the problem, it would be ok to decide that a tiny bias is not a problem, depending on your application (like: in a simulation you don't want that, in a game it's acceptable) -- and, of course, document such a decision in comments. –  Oct 10 '15 at 19:38
  • And we're probably going far beyond here, given a line like `getRand(); // kick-start the random numbers` in this professor's code, that really demonstrates his "expertise" ... –  Oct 10 '15 at 19:43

2 Answers2

1

This is the most obvious implementation:

int rollDie(int number_of_sides) {
    return (getRand() * number_of_sides) + 1;
}

But getRand() using float introduces a number of problems (as mentioned in the comments). In real world code, the usual rand() % max + 1 should be sufficient.

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
  • This code is the correct answer , but what really confuses me is that isnt this function saying: take the value of getrand() which is between 0 and 1 then multiply it with the number of sides then add one so if the value is 0.9 and then its a 6 sided dice it will do (0.9 * 6)+1 which is 7? – CcoderBeginner Oct 11 '15 at 12:03
  • sorr i mean int (0.999*6)+1 =7 – CcoderBeginner Oct 11 '15 at 12:16
  • 1
    @user276586 `(0.999*6)+1=6.994`, which, when converted to an `int`, is `6`. The fractional part is truncated. – PC Luddite Oct 11 '15 at 15:55
1

First, I'll take the freedom to call the function rollDice() now.

For anyone who is interested, there is already an answer showing good code for "rolling a dice". It should be referenced here because answers should be valuable for future readers.

Your professor obviously wants you to write something like

int rollDice(int max) {
    return (int)(getRand() * max) + 1;
}
Community
  • 1
  • 1