Is this programmer incorrect?

closed account (367kGNh0)
I noticed a programmer give this example

1
2
3
4
5
6
7
8
9
10
11
12
13
const char *activities[] = {
    "Diving",
    "Lapping",
    "Cannonballing",
    "Butterfly kicking",
    "Paddling",
    "Swimming",    
    "sidestroke",
    "front crawl",
    "breast stroke",
};
cout<<"We are gonna do " << activities[rand()%9];


is his rand()%9; incorrect because in programming the program starts from zero? It should be rand()%8 correct?
Last edited on
rand%9 will give you random numbers from 0 to 8. So this looks correct.
What do you mean by "in programming the program starts from zero"?

How much is 0 % 9? How about 54 % 9?
closed account (367kGNh0)
rand%9 will give you random numbers from 0 to 8. So this looks correct.

I see, 0 being "diving"?




What do you mean by "in programming the program starts from zero"?

How much is 0 % 9? How about 54 % 9?

I mean by default

We get what you mean, but a clearer way of saying it is that "In C++, the index of the first element of an array is 0, not 1 as in some other programming languages".
closed account (367kGNh0)
We get what you mean, but a clearer way of saying it is that "In C++, the index of the first element of an array is 0, not 1 as in some other programming languages".
Thank you for the advice
the programmer does have one flaw.
he should say
const int act_size = 9;
const char *activities[act_size] …
and rand()%act_size;

its the same, but a sized to data array mixed with a magic number to process it is asking for trouble when someone edits the code later. Maybe next year, they removed cannonball from the list:

const char *activities[] = {
"Diving",
"Lapping",
// "Cannonballing", //this was too dangerous and no longer done here
"Butterfly kicking",
"Paddling",
"Swimming",
"sidestroke",
"front crawl",
"breast stroke",
};

cout<<"We are gonna do " << activities[rand()%9]; //what happens when rand returns 8?
closed account (367kGNh0)
Post eradicated. I took something out of context. Mistake is my own
Last edited on
closed account (z05DSL3A)
Rascake wrote:
This is not the lounge.
it is also not in the beginners section where it belongs...
but anyway, reread what jonnin said in his post.
Even better:
1
2
3
4
5
6
7
8
9
template <typename T, size_t N>
size_t array_size(T (&)[N]){
    return N;
}

//...

static_assert(array_size(activities) <= RAND_MAX + 1, "Oops! Better use some other algorithm!");
cout<<"We are gonna do " << activities[rand() % array_size(activities)];
Last edited on
@helios, Should you maybe do something like (long long)RAND_MAX + 1 so that a RAND_MAX of the highest signed int won't overflow?
I don't really trust casting for this. If MAX_RAND is close to the upper limit of its type, I'd much rather do
array_size(activities) && array_size(activities) - 1 <= RAND_MAX
Last edited on
> so that a RAND_MAX of the highest signed int won't overflow?

Since RAND_MAX is an integral constant expression,
compilers would generate a warning if there is an integer overflow.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <limits>

int main()
{
    const int rand_max = std::numeric_limits<int>::max() ;
    
    // GNU: *** warning: integer overflow in expression of type 'int' results in '-2147483648'
    // LLVM: *** warning: overflow in expression; result is -2147483648 with type 'int'
    // Microsoft: *** warning: '+': integral constant overflow
    const int k = rand_max + 1 ;
    
    return k == 0 ;
}

http://coliru.stacked-crooked.com/a/02e3faa30814ea2a
https://rextester.com/UNUJ62469
Good compilers generate a warning.

Some compilers will just say UB and ignore you.
Topic archived. No new replies allowed.