Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Roll n fair dice

C (gcc), 48 43 bytes s;r(n,m){s+=rand()%m+1;return--n?r(n,m):s;} Try it online! Previous 48 bytes version using loop: i,s;r(n,m){for(;i<n;i++)s+=rand()%m+1;return s;}

posted 2y ago by Lundin‭  ·  edited 2y ago by Lundin‭

Answer
#3: Post edited by user avatar Lundin‭ · 2022-05-30T09:07:05Z (over 2 years ago)
  • # [C (gcc)], <del>48</del> 45 bytes
  • <!-- language-all: lang-c -->
  • i,s;r(n,m){s+=rand()%m+1;return--n?r(n,m):s;}
  • [Try it online!][TIO-l3si606q]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-l3si606q]: https://tio.run/##VY29DsIgFEZ3noLUNIEUjDo4FH9exKUC6k3KrblQl6bPjkUH43rO@fJZfbc2rwBtPzrPDzG5Hq7rx4n9WILg/8lSwVBQBhUNCVRBTrE5UodOyDo0W0M@jYRa4/mr22jmDJh46AC5eA3gJJsY5/EzKh9iI6VZyJOW7iaq2l2wUpzETu2LmfMb "C (gcc) – Try It Online"
  • ---
  • Previous 48 bytes version using loop:
  • `i,s;r(n,m){for(;i<n;i++)s+=rand()%m+1;return s;}`
  • # [C (gcc)], <del>48</del> 43 bytes
  • <!-- language-all: lang-c -->
  • s;r(n,m){s+=rand()%m+1;return--n?r(n,m):s;}
  • [Try it online!][TIO-l3sib2ai]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-l3sib2ai]: https://tio.run/##VY29DsIgFEZ3noLUNLmkYNTBofjzIi4VUG9Sbg1Ql6bPjkUH43rO@fIZdTcmr5BMP1rHDzHZHq/rx4n9WELv/slS4VBQjjoASS@m2BxDRxZE7ZutDi6NgZSi81e3Uc8ZKXHfIXF4DWgFmxjn8TMqD7ARQi/kGZbuBlVtL1RJHmAn98XM@Q0 "C (gcc) – Try It Online"
  • ---
  • Previous 48 bytes version using loop:
  • `i,s;r(n,m){for(;i<n;i++)s+=rand()%m+1;return s;}`
#2: Post edited by user avatar Lundin‭ · 2022-05-30T09:06:04Z (over 2 years ago)
Swapping loop for recursion shaved 3 bytes
  • # [C (gcc)], 48 bytes
  • <!-- language-all: lang-c -->
  • i,s;r(n,m){for(;i<n;i++)s+=rand()%m+1;return s;}
  • [Try it online!][TIO-l3shtp7u]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-l3shtp7u]: https://tio.run/##Vc1BDoIwEIXhPaeYYEimaTWycVP0JG6wBZ2EDmRa3BDOXsGNcfu9P3nu@HQuH4jdMPsOmpj8QI/T61b8LFHo/mWraNwpk4lWkE1QSz8KWmrYktYq6qu07FFVQddWujQLQ7RrJk4QWmLA90heFUsBEL/pfoNnpewmk2xdj2Xl71waEKzNZV/W/AE "C (gcc) – Try It Online"
  • Not a lot of ways to make it more compact in C (gcc). Pass by reference version yielded same symbol count:
  • `i;r(n,m,int*q){for(*q=0;i<n;i++)*q+=rand()%m+1;}`
  • This would also work but also same count:
  • `i,s;r(n,m){for(;i<n;i++)s+=rand()%m;return s+n;}`
  • # [C (gcc)], <del>48</del> 45 bytes
  • <!-- language-all: lang-c -->
  • i,s;r(n,m){s+=rand()%m+1;return--n?r(n,m):s;}
  • [Try it online!][TIO-l3si606q]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-l3si606q]: https://tio.run/##VY29DsIgFEZ3noLUNIEUjDo4FH9exKUC6k3KrblQl6bPjkUH43rO@fJZfbc2rwBtPzrPDzG5Hq7rx4n9WILg/8lSwVBQBhUNCVRBTrE5UodOyDo0W0M@jYRa4/mr22jmDJh46AC5eA3gJJsY5/EzKh9iI6VZyJOW7iaq2l2wUpzETu2LmfMb "C (gcc) – Try It Online"
  • ---
  • Previous 48 bytes version using loop:
  • `i,s;r(n,m){for(;i<n;i++)s+=rand()%m+1;return s;}`
#1: Initial revision by user avatar Lundin‭ · 2022-05-30T08:56:45Z (over 2 years ago)
# [C (gcc)], 48 bytes

<!-- language-all: lang-c -->

    i,s;r(n,m){for(;i<n;i++)s+=rand()%m+1;return s;}

[Try it online!][TIO-l3shtp7u]

[C (gcc)]: https://gcc.gnu.org/
[TIO-l3shtp7u]: https://tio.run/##Vc1BDoIwEIXhPaeYYEimaTWycVP0JG6wBZ2EDmRa3BDOXsGNcfu9P3nu@HQuH4jdMPsOmpj8QI/T61b8LFHo/mWraNwpk4lWkE1QSz8KWmrYktYq6qu07FFVQddWujQLQ7RrJk4QWmLA90heFUsBEL/pfoNnpewmk2xdj2Xl71waEKzNZV/W/AE "C (gcc) – Try It Online"

Not a lot of ways to make it more compact in C (gcc). Pass by reference version yielded same symbol count:

`i;r(n,m,int*q){for(*q=0;i<n;i++)*q+=rand()%m+1;}`

This would also work but also same count:

`i,s;r(n,m){for(;i<n;i++)s+=rand()%m;return s+n;}`