gh-card | : octocat : GitHub Repository Card for Any Web Site | Static Site Generator library
kandi X-RAY | gh-card Summary
kandi X-RAY | gh-card Summary
:octocat: GitHub Repository Card for Any Web Site
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of gh-card
gh-card Key Features
gh-card Examples and Code Snippets
Community Discussions
Trending Discussions on gh-card
QUESTION
I am trying to write a code which randomizes numbers between 1 and 14 (symbolizing one suit of a deck of cards). The code should store the values in an array with a limit of 52. Each number can only be stored 4 times (as there are 4 suits in a deck of cards). So, in the end, I should be displaying two randomized decks for person_a and person_b.
My problem is that the randomized decks for person_a and person_b are the same. I have no idea why. I tried seeding using srand(), and using rand() for the random number. Can someone help?
Also, I know my code is really messy and terrible. I'm sorry - this is my first time taking a C course. Below is the code:
...ANSWER
Answered 2017-Apr-02 at 00:50Your problem stems from the fact that cards
is declared as a static
array in the randomize_deck()
function. So, the first call to randomize_deck()
fills this array with random cards, returning a pointer to cards
. Then the second call to randomize_deck()
fills the same static
array with new random cards, and returns a pointer to the same static
array. After all of this, both person_a
and person_b
point to the same static
array.
One solution is to change the randomize_deck()
function to accept an array argument, with a return type of void
. Also, it would be best to pass the size of the array to randomize_deck()
, instead of relying on a global constant. And note that in the code below I have changed the array index variables to type size_t
, which is an unsigned
integer type guaranteed to hold any array index, and the correct type for array indices.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install gh-card
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page