Thread: randomazation

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    randomazation

    I read the tutorial on rand, but it didnt make much sense
    (sorry RoD, thanks anyway) can someone explain it to me
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  2. #2
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Generate random number.
    Returns a pseudo-random number in the range from 0 to RAND_MAX constant. This is generated by an algorithm that returns a series of non-related numbers each time is called. This algorithm should be initialized to different starting points using function srand to generate more realistic random numbers.
    RAND_MAX is a constant defined in stdlib.h. Its default value is implementation defined.


    Parameters.

    (none)

    Return Value.
    An integer value between 0 and RAND_MAX.


    Code:
    /* rand example */
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
      /* initialize random generator */
      srand ( time(NULL) );
    
      /* generate some random numbers */
      printf ("A number between 0 and RAND_MAX (%d): %d\n", RAND_MAX, rand());
      printf ("A number between 0 and 99: %d\n", rand()%100);
      printf ("A number between 20 and 29: %d\n", rand()%10+20);
    
      return 0;
    }
    Code:
    Output:
    A number between 0 and RAND_MAX (32767): 30159
    A number between 0 and 99: 72
    A number between 20 and 29: 23
    thus rand()%25 would be a random number between 0 and 24, both included.


    A good way to generate almost-true random numbers is to initialize the random algorithm using srand with the current time in seconds as parameter, as obtained from time function included in <time.h>.
    And, generally, a good way to get an integer random number between a range is to perform a module (%) operation on a result provided by rand():
    Last edited by xxxrugby; 02-13-2005 at 07:05 PM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Looks for posts on this I had one called "randomizing" I asked the same question. I'll be nice and briefly explain it

    Here:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <ctime> //need this
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        srand((unsigned int)time(0) ); //seeds the generator
        int number = rand()%10; //generates a number between 1 and 10
        
        cout<<number;
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    My computer is awesome.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I have a question. I want to generate a lot of random numbers. Random numbers between 1-20 in groups of six where none of the numbers are the same. How would I go about doing this?
    My computer is awesome.

  5. #5
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    int number = rand()%10; //generates a number between 1 and 10
    Nope...it generates a number 0 - 9.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cerin
    I have a question. I want to generate a lot of random numbers. Random numbers between 1-20 in groups of six where none of the numbers are the same. How would I go about doing this?
    How the hell do you think it would be done? Do you actually ever think about how you might figure something out on your own, or do you just wait for every one else to do everything for you?

    That was a rhetorical question.

    Here I got to teach yet another person how to think for themselves.

    1) How would you yourself generate groups of six numbers?
    1a) Break that down into small steps.
    1b) Write it out.
    2) How would you turn those steps into code?

    3) How would you ask for help with that code after you've tried on your own?
    (Here's a hint, it's in the forum Announcements.)

    Personally, I'd grab six D20s and roll them. If I didn't have six of them, which I do, I'd probably roll one D20, six times. Hm... I bet I could think of how to do the same thing in C++ six times. For instance, while I wasn't done, I'd repeat that same task over and over again. You know, like in a loop...

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

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    I guess for something that only uses 6 numbers you could do something sloppy like add each random number you generate to an array and for each generation check that array to see if the new number is in that array. If not, you've got a unique random number.

    That is only going to be okay with a small array like in your case, with 6 numbers. If you're dealing with more numbers, I might generate a huge array of random numbers and then sort them deleting duplicates. Then shuffle.

    I'm fairly n00b myself, however, and there is probably a MUCH more efficient way. But one thing I have learned is to think for myself - whether or not I'm wrong is not relevant. If I get a workable solution, that's great. Then, as I get better, I can improve it.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    A fun way is to create a vector of (n) ints, from 0 to (n - 1). To get a unique random number, find a random number from 0 to (size of vector - 1), and use it to index the vector; copy the number into your array or whatever, then remove it from the vector. Repeat until you have all the unique numbers you want in that range, or until you run out of numbers.

    It isn't super efficient, but the absolute worst-case scenario (n^2 / 2 copy operations) is better than the purely theoretical worst-case scenario of the other method (infinity randoms)
    Last edited by Hunter2; 02-14-2005 at 11:23 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed