Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    Question Random Numbers

    Hey, A uni project I'm on basically requires us to design a mars rover simulation. All command line. I'm on this function that actually creates the map, and am trying to set up whether or not the rover can move over each square on the grid. I've got a rand() in there with 1's or 0's, IE yes or no, but it's giving me a fairly even playing field, i was wondering if there was any way in biasing the results so, say, it was twice as likely to be a 1 than a 0.? Cheers fo any help (or any ideas of any kind )

    code's below so far, though its pretty standard

    Cheers

    Mitch

    P.s. - there are to be a number of objects on the land, be they rocks, pools, aliens, etc, thats what the ObjId is

    P.p.s, i have thought about for example having the rand make four variables, and having 0, 1 and 2 equal passable land and 3 equalling non-passable land, was just looking to a better, more c-based way.

    Code:
    void make_map(int mapsizex, int mapsizey)
    {
    
    	location **map;
    
    	int row, col;
    
    	//create the map, dynamically allocating memory for each location
    
    	//allocate memory for rows
        
    	map=(location**)(malloc((mapsizex)*sizeof(location*)));
        
    	//allocate memory for each row of the matrix
        
    	for(row=0;row<(mapsizex);row++)
    		map[row]=(location*)(malloc((mapsizey)*sizeof(location)));
    
    
    	//Matrix of Structures complete
    
    	  srand(time(NULL));
    	  
    
    	  for(row=0; row<mapsizex; row++)
    		  for(col=0; col<mapsizey; col++)
    			  map[row][col].ObjId=rand()%18;
    
    
    	  for(row=0; row<mapsizex; row++)
    		  for(col=0; col<mapsizey; col++)
    			  printf("%d ", map[row][col].ObjId);
    
          printf("\n\n\n");
    		  
    	  for(row=0; row<mapsizex; row++)
    		  for(col=0; col<mapsizey; col++)
    			  map[row][col].Movable=rand()%2;
    
     	  for(row=0; row<mapsizex; row++)
    		  for(col=0; col<mapsizey; col++)
    			  printf("%d ", map[row][col].Movable);
    
    }
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    [email protected]
    ~~~~~~~~~~

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok say you want to bias 1 so its twice as likely as 0 to appear.

    Instead of ranging your randoms from 0 to 1, change it to 0 to 2. If its >= 1 return 1 if not return 0.

    Also FYI:
    In Numerical Recipes in C: The Art of Scientific Computing
    (William H. Press, Brian P. Flannery, Saul A. Teukolsky,
    William T. Vetterling; New York: Cambridge University
    Press, 1992 (2nd ed., p. 277)), the following comments are
    made:
    "If you want to generate a random integer between 1
    and 10, you should always do it by using high-order
    bits, as in

    j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

    and never by anything resembling

    j=1+(rand() % 10);

    (which uses lower-order bits)."
    From the man page for rand()

    Edit: Forgot to mention to do what I mentioned you'll need to add a function that gets a random number.
    Last edited by Thantos; 02-27-2004 at 01:06 PM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Re: Random Numbers

    Originally posted by spudtheimpaler
    I've got a rand() in there with 1's or 0's, IE yes or no, but it's giving me a fairly even playing field, i was wondering if there was any way in biasing the results so, say, it was twice as likely to be a 1 than a 0.? Cheers fo any help (or any ideas of any kind )
    Here is a quick hack of an idea.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int foo(void) /* even odds */
    {
       return rand() > RAND_MAX / 2;
    }
    
    int bar(void) /* twice as likely to be 1 as 0 */
    {
       return rand() > RAND_MAX / 3;
    }
    
    void test(int (*function)(void), int cycles)
    {
       int i, result[2] = {0,0};
       double x,y;
       for ( i = 0; i < cycles; ++i )
       {
          ++result[function()];
       }
       x  = result[0] * 100.0 / cycles;
       y  = result[1] * 100.0 / cycles;
       printf("0: %d (%.2f%%), 1: %d (%.2f%%)\n", result[0], x, result[1], y);
    }
    
    int main(void)
    {
       srand(time(0));
       test(foo, 10000);
       test(bar, 10000);
       return 0;
    }
    
    /* my output
    0: 4979 (49.79%), 1: 5021 (50.21%)
    0: 3302 (33.02%), 1: 6698 (66.98%)
    */
    [edit]A different spin on the above.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int likelihood(int difficulty)
    {
       return rand() < RAND_MAX / (difficulty + 1);
    }
    
    void test(int (*function)(int), int n)
    {
       int i, result[2] = {0,0}, cycles = 1000000;
       printf("likelihood = 1:%d |", n);
       for ( i = 0; i < cycles; ++i )
       {
          ++result[function(n)];
       }
       for ( i = 0; i < sizeof result / sizeof *result; ++i )
       {
          double percent = result[i] * 100.0 / cycles;
          printf(" %d = %6d (%5.2f%%)%c", i, result[i], percent, ",\n"[i]);
          fflush(stdout);
       }
    }
    
    int main(void)
    {
       int i;
       srand(time(0));
       for ( i = 1; i < 10; ++i )
       {
          test(likelihood, i);
       }
       return 0;
    }
    
    /* my output
    likelihood = 1:1 | 0 = 499869 (49.99%), 1 = 500131 (50.01%)
    likelihood = 1:2 | 0 = 666451 (66.65%), 1 = 333549 (33.35%)
    likelihood = 1:3 | 0 = 749825 (74.98%), 1 = 250175 (25.02%)
    likelihood = 1:4 | 0 = 800192 (80.02%), 1 = 199808 (19.98%)
    likelihood = 1:5 | 0 = 833687 (83.37%), 1 = 166313 (16.63%)
    likelihood = 1:6 | 0 = 856950 (85.69%), 1 = 143050 (14.30%)
    likelihood = 1:7 | 0 = 875569 (87.56%), 1 = 124431 (12.44%)
    likelihood = 1:8 | 0 = 888736 (88.87%), 1 = 111264 (11.13%)
    likelihood = 1:9 | 0 = 899771 (89.98%), 1 = 100229 (10.02%)
    */
    [/edit]
    Last edited by Dave_Sinkula; 02-27-2004 at 11:00 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Thantos
    Also FYI:
    In Numerical Recipes in C: ... the following comments are made:
    "If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in
    j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

    and never by anything resembling

    j=1+(rand() % 10);

    (which uses lower-order bits)."
    From the man page for rand()
    What I dislike about this type of suggestion is the requirement of blind acceptance. Why this is better should be part of the explanation. If someone asks you why you did it that way, and unacceptable reason to me is "the book said so."
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The reason why is in the quote, using rand() % 10 only uses the low order bits while the other method uses the high order bits. If someone wants it explained further they can do more research on it.

    I posted it as mearly informational.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    Thank you all

    Cheers Thantos, Thats prob what i'll end up doing (i think i may have mentioned that method in my pps though).

    As for all the high end low end stuff, I've barely been programming 6 months, and its a uni course IE a couple of hours a week. Its interesting seeing that sort of info and I'm glad i know it though i couldn't possibly include it. My code has to be transportable and be able to be combined in another persons project. I think i'd blow them away

    Cheers for your help guys

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    [email protected]
    ~~~~~~~~~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. 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
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM