Generating a Random Number from an array for a blackjack simulator-Matlab

Click For Summary
SUMMARY

The discussion focuses on generating a random card for a blackjack simulator using MATLAB. The user attempted to utilize the randi function with an array of possible card values, but faced issues in drawing a random card. The correct approach involves using the randi function with the length of the array to index the card values properly. The solution is to modify the code to CardValue = x(randi(length(x))) to ensure a valid random selection from the array.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with arrays in MATLAB
  • Knowledge of the randi function in MATLAB
  • Understanding of blackjack rules and card values
NEXT STEPS
  • Explore MATLAB array indexing techniques
  • Learn about random number generation in MATLAB
  • Study the implementation of game logic in MATLAB for card games
  • Investigate MATLAB's built-in functions for simulating games
USEFUL FOR

This discussion is beneficial for MATLAB programmers, game developers, and students working on simulations or projects related to card games, particularly blackjack.

Matt12
Messages
2
Reaction score
0
Generating a Random Number from an array for a blackjack simulator--Matlab

I have a homework problem where I have to generate a single hand of blackjack to a player and allow the player to hit or hold. I'm ahving trouble generating the random card.

My attempt:

x = [1,2,3,4,5,6,7,8,9,10,10,10,10,11]; %the possible random numbers
CardValue = randi(x,1) %this is the problem, I want to draw a random card from the array



Thanks for any help!
 
Physics news on Phys.org


Also, this is my first post so if I'm in the wrong category or need to tell more, let me know!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
7K
  • · Replies 21 ·
Replies
21
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
25
Views
4K