Thread: How to randomize?

  1. #1
    Supar
    Guest

    How to randomize?

    Alrite...im using MSVC++...i am also teaching myself...and am not that far along...but i really want to get this randomizing thing...and the examples given in the FAQ didnt work...with the exception of this one:

    #include <stdlib.h>
    int rand(void);

    #include <stdlib.h> //for rand
    #include <stdio.h> //for printf

    int main(void)
    {
    randomize();
    printf("A random number from 0 to 99: %d", rand() % 100);
    return 0;
    }

    So if someone could either show me a different way...or explain to me why this one works that would be great ;P

  2. #2
    Unregistered
    Guest
    /*#include <stdlib.h>
    int rand(void);*/ take that part out ;P

    #include <stdlib.h> //for rand
    #include <stdio.h> //for printf

    int main(void)
    {
    randomize();
    printf("A random number from 0 to 99: %d", rand() % 100);
    return 0;
    }

  3. #3
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    #include <iostream.h>

    int main()
    {
    int this = rand() %11;
    return 0;
    }




    err....something like that i dont remember really good
    but 11 is the number that randomizes from 1 -10 (im not sure why the extra 1 isnt counted) but thats sorta the idea. You should ask other poeple how do that
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>but 11 is the number that randomizes from 1 -10

    you mean;


    Code:
    int this = (rand() %10)+1;

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    before u use rand() u have to use srand(value) and substitute a value for value. If u don't want the same results everytime, use this inside srand: time(NULL) and make sure to include time.h

  6. #6
    Supar
    Guest
    ah thanks...havent tried it yet...but im sure it works ;P

  7. #7
    Supar
    Guest
    yes it works! great! now just one more thing...(yea...lol) how do you set a lowest number? like if i wanted to randomize between 45 and 85 or something like that? i looked at the FAQ again...and it had this as well...but it didnt explain it well enough to me(yea im a newbit when it comes to programming~)

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    rand()%range+minimum

    where:
    range=highest number-lowest number+1
    minimum=lowest possible number

  9. #9
    Supar
    Guest
    wow you guys are great...heh mebbe i should register...i think i may start frequenting this board~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomize array blackjack c program
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 02:05 PM
  2. don't want to randomize two alike numbers
    By randomizer in forum C++ Programming
    Replies: 8
    Last Post: 05-26-2005, 07:42 PM
  3. randomize hex digits
    By wazilian in forum C Programming
    Replies: 3
    Last Post: 12-14-2002, 03:20 AM
  4. Problem with Visual C++ ( randomize function )!
    By marCplusplus in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 01:01 PM
  5. Randomize Number Function
    By beyonddc in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 04:31 AM