Question about using rand

This is a discussion on Question about using rand within the C++ Programming forums, part of the General Programming Boards category; I am making a text-football game, and decided to use rand to handle most of the tasks. Such as field ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Question Question about using rand

    I am making a text-football game, and decided to use rand to handle most of the tasks. Such as field goals, rushing yards, etc. Like a 50%chance of making a field goal from 40 yards. But can you only use rand once? I didn't want to try it because it would be a waste of time then. Here is what I am using for the coin toss:
    Code:
    srand(time(0));
        (rand()%2)+1;
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You seed it with srand once (so you need to make sure you don't have that call in any loops, or repetitively called functions). I like to have it as early as possible in the main() function. Then you can call rand() whenever you need another random number.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    It seems people read the FAQ at about rand()%100 == 0 intervals.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    There's a FAQ?
    Woop?

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Yes, there is a FAQ. I read it but didn't help too much. The part where you scale the numbers using rand, didnt even work. And my book doesn't say much about either.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> But can you only use rand once?
    Use rand as often as you want. Only use srand once (before any calls to rand), but rand itself should be called for each random number you need.

    >> I didn't want to try it because it would be a waste of time then.
    When in doubt, trying things is often a very good idea.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Sentral
    The part where you scale the numbers using rand, didnt even work.
    Somehow I doubt it. Also, there are actually two FAQs on it, not one.


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

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I'd have to agree... I tried the code from this entry:
    Code:
    #include <iostream>
    #include <ctime>
    
    int main(void)
    {
            int i;
    
            srand(time(NULL));
            i = rand();
            std::cout <<"Your random number is " <<i <<std::endl;
            std::cout <<"This compiler can generate random numbers from 0 to "
                      <<RAND_MAX <<std::endl;
    
            return(0);
    }
    and got this:
    jshao@MCP ~/Programming/C++ $ g++ test.cpp -Wall -W -ansi -o test.exe
    jshao@MCP ~/Programming/C++ $ ./test.exe
    Your random number is 1232683022
    This compiler can generate random numbers from 0 to 2147483647
    and if you follow the link on the bottom, it shows you (correctly) how to select a range of numbers.
    Last edited by major_small; 07-18-2005 at 08:04 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > The part where you scale the numbers using rand, didnt even work.
    At which point the correct response would be.

    Hey I tried this from the FAQ and got these results (or non-results, or compiler errors, or ...)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    The Modulus Operator %

    ...didnt even work.
    Here's how it works - The modulus operator (the percent-sign) gives you the remainder after division. So for example, if you divide a number by 5, the remainder must be less than 5.

    If you divide by 2, the remainder will be zero or one... a 50/50 chance... kinda like a coin-flip!

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    I wonder if the OP did rand()%2 and was saying didn't work on the basis that they got 0101010101010101.... as the sequence?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Question about the rand() function
    By V4D3R in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2007, 06:37 PM
  3. rand() question
    By Spectrum48k in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2003, 08:08 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. rand() question
    By abrege in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2002, 07:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21