Thread: using random number generator

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

    using random number generator

    hello I decided to make a little simple 21 game and am stuck on how can i use a variable that stores a random number everytime, for example i set a variable called total= 1+rand()%MAX but each time i call the variable it only gives back the first random number that it gets.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Hmm...I'm slightly confused about your question. Perhaps you mean to use some sort of arrays and/or loops for this.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the FAQ, Luke!

    Cprogramming.com FAQ > Generate random numbers?

    My guess is that he's talking about not having used srand properly, and getting the same number every time.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by anduril462 View Post
    Hmm...I'm slightly confused about your question. Perhaps you mean to use some sort of arrays and/or loops for this.
    for example I need to have random numbers given to the player and the computer (numbers meaning cards). but I cant think of a way to keeping count of the random numbers so that when someone busts over 21 the game can end.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by camel-man View Post
    for example I need to have random numbers given to the player and the computer (numbers meaning cards). but I cant think of a way to keeping count of the random numbers so that when someone busts over 21 the game can end.
    The whole point of rand is to give you the next number off the deck. It's your job to put those numbers somewhere, and maybe add them up if you want a total.

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by tabstop View Post
    The whole point of rand is to give you the next number off the deck. It's your job to put those numbers somewhere, and maybe add them up if you want a total.
    yes but how can you add them up? by setting rand equal to a variable and then variable+=variable?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't randomly generate cards, instead put all 52 cards in an array in a predetermined order (say by suit, then value), and shuffle the deck, using a shuffling algorithm. Simply count the cards as they're dealt, moving through the array so you know what card you're giving out next.

    As for each player's hand, you need to store the cards they've received in an array or linked list. Then, you can write a total_hand() function that goes through the array/list and adds up the total value of the cards. If that value is over 21, you busted. Remember, aces can be 1 or 11, so you'll have to work that in there.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by camel-man View Post
    hello I decided to make a little simple 21 game and am stuck on how can i use a variable that stores a random number everytime, for example i set a variable called total= 1+rand()%MAX but each time i call the variable it only gives back the first random number that it gets.
    First of all, decks of cards do not lend themselves well to random numbers... every card has to be unique and random numbers do repeat.

    Your best bet is to make an array[52] and load each element with it's value... card[0]=0, card[1]=1 etc... in a loop. Then random shuffle the deck.

    There are several ways of assigning the values to the cards... for example : card[i] % 13 will give you it's rank in the suit... card[i]/13 will give you the suit... and so on.

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    nice nice niceee, I was not thinking out of the box what so ever I am definitely going to do it that way, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. random number generator
    By morrissey999 in forum C Programming
    Replies: 18
    Last Post: 05-01-2011, 04:53 PM
  3. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 07:34 AM
  4. random number generator
    By Verbenaca in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2005, 08:05 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM