Random Numbers in Allegro.

This is a discussion on Random Numbers in Allegro. within the C++ Programming forums, part of the General Programming Boards category; I need to generate random numbers for a function in my game. If the number generated is even the player ...

  1. #1
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29

    Arrow Random Numbers in Allegro.

    I need to generate random numbers for a function in my game. If the number generated is even the player gets an item, if its odd it fails. At the moment my character needs to cut down some wood. I tried this:
    Code:
                     A:                        
                               if(key [KEY_ENTER])
                               {
                                      (srand(static_cast<unsigned>(time(0))));
                                      while(true)
                                      {
                                                 rand()%1+1;
                                  
                                  if(rand()%2==0)
                                  {       
                                      textprintf_ex(backbuffer, font, 3, 450, makecol(255, 255, 255), 0, "You obtain a piece of wood!");
                                      wclvlxp+=10;
                                      readkey();
                                  }
                                  else if(rand()%2==1)
                                  {
                                      textprintf_ex(backbuffer, font, 3, 450, makecol(255, 255, 255), 0, "You fail cutting the tree.");
                                      readkey();
                                      goto A;
                                  }      
                               }
                               }
    but after i go to the tree and click enter, the game freezes. Anyone know how to fix this?
    "A Computer in every desk and in every home, running Microsoft software."

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Fix things like say the indentation?

    Or the poor use of goto to escape a while loop ?

    Or what is this!?
    > rand()%1+1;

    rand() % 2 (on some machines) has been known to alternate 0,1,0,1,0,1,0,1 (ad infinitum)
    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.

  3. #3
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    Quote Originally Posted by Salem View Post
    Fix things like say the indentation?

    Or the poor use of goto to escape a while loop ?

    Or what is this!?
    > rand()%1+1;

    rand() % 2 (on some machines) has been known to alternate 0,1,0,1,0,1,0,1 (ad infinitum)
    Thats something I recovered from a text based game I made long ago. The concept is the same.
    How can I generate random numbers correctly in Allegro?
    "A Computer in every desk and in every home, running Microsoft software."

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    rand()%1 + 1 isn't very random, being guaranteed to be constantly 1.

    Search the FAQ on random numbers.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by godly 20 View Post
    Thats something I recovered from a text based game I made long ago. The concept is the same.
    How can I generate random numbers correctly in Allegro?
    Try using x = (rand() %10) & 1); Which will give you 0 or 1 but not as an alternating sequence.

    Also when doing things like you listed above... generate 1 random number at the top and act on it... otherwise you're likely to get some truly weird stuff going on.

  6. #6
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    Quote Originally Posted by CommonTater View Post
    Try using x = (rand() %10) & 1); Which will give you 0 or 1 but not as an alternating sequence.

    Also when doing things like you listed above... generate 1 random number at the top and act on it... otherwise you're likely to get some truly weird stuff going on.
    I did this:
    Code:
    int r;
    if(key [KEY_ENTER])
                               {
                                      r=(rand()%10) & 1);
                                      if(r%2==0){
                                      textprintf_ex(backbuffer, font, 3, 450, makecol(255, 255, 255), 0, "You get some wood!");
                                      wclvlxp+=10;
                                      readkey();}
                               }
    but it doesn't compile. Help!
    "A Computer in every desk and in every home, running Microsoft software."

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Sorry, I don't fancy playing a game of "guess the error messages", especially since what you post won't compile.
    So you're just going to have to tell us.
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Simple bracketing mismatch... you should be able to spot these easily...

    Code:
      r=(rand()%10) & 1;

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    .

    sort out your naming conventions.. it will save you a lot of pain! wxyyjzkcl = 10 Or whatever you wrote, is a bad idea :-)
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM

Tags for this Thread


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