Random isn't Random

This is a discussion on Random isn't Random within the C Programming forums, part of the General Programming Boards category; I'm writing a boggle application in C and I need to randomize the values of 2 arrays. However, both arrays ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Random isn't Random

    I'm writing a boggle application in C and I need to randomize the values of 2 arrays. However, both arrays get 'randomized' the same way (if i am using the same random code). I have tried both (rand() % #MAX_NUMBER_HERE) and (int)((rand() / MAX_RAND) * #MAX_NUMBER_HERE). Can anyone see what is wrong? I'm pretty sure that the numbers being generated are in fact random...

    On a side note, is the last for loop the correct way to print an array in the form of:
    A B C D
    E F G H
    I J K L
    M N O P

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define diceA {'Q','U','M','H','N','I'}
    #define diceB {'O','O','B','B','J','A'}
    #define diceC {'C','M','U','O','T','I'}
    #define diceD {'T','Y','E','L','R','T'}
    #define diceE {'E','E','G','N','A','A'}
    #define diceF {'O','T','W','O','T','A'}
    #define diceG {'E','E','N','H','W','G'}
    #define diceH {'H','A','P','S','O','C'}
    #define diceI {'I','T','S','T','Y','D'}
    #define diceJ {'T','O','E','S','S','I'}
    #define diceK {'E','R','L','I','D','X'}
    #define diceL {'N','L','N','H','Z','R'}
    #define diceM {'N','E','E','I','U','S'}
    #define diceN {'P','S','A','F','K','F'}
    #define diceO {'R','E','T','W','V','H'}
    #define diceP {'E','Y','L','D','V','R'}
    
    char allDice[16][6] = {diceA,diceB,diceC,diceD,diceE,diceF,diceG,diceH,diceI,diceJ,diceK,diceL,diceM,diceN,diceO,diceP};
    
    int main (int argc, const char * argv[]) {
    	char lettersUp[16] = {0};
    	for (int i = 0; i < 16; i++) {
    		lettersUp[i] = allDice[i][(rand() % 6)];	//NEED A BETTER RANDOM GENERATOR???
    	}
    	
    	char board[4][4] = {0};
    	int used[16] = {0};
    	for (int i = 0; i < 4; i++) {
    		for (int j = 0; j < 4; j++) {
    			retry:;
    			int pointer = (rand() % 16);			//NEED A BETTER RANDOM GENERATOR???
    			if (used[pointer] != 1) {
    				board[i][j] = lettersUp[pointer];
    				used[pointer] = 1;
    			}
    			else {
    				goto retry;
    			}
    		}
    	}
    	
    	for(int i = 0; i < 3; ++i)
    	{
    		for(int j = 0; j < 3; ++j)
    		{
    			printf("%s", board[i][j]);
    		}
    		putchar('\n');
    	}
    }

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Because you don't seed it with srand().

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yes... as msh said.

    The pseudo-random number generator will generate the same sequence of numbers each time the program is run. This is because of two reasons:

    1) It's "pseudo random". That is, a deterministic method is used... There is no truly random element inside a computer. Only algorithms. Unless someone puts in real dice and a video camera. LOL

    (Or a noisy thermistor and an A/D converter, and more fancy algorithms to de-cluster the probability so that it has an even envelope.)

    2) The repeatability of the sequence helps in testing programs. Predictability on the computer's part means different scenarios can be tried... your program can be tested many times. Thus eliminating one major headache when attempting to track down some flaky behavior. Only the programmer should be the "flake". Not the machine. ;-)

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Unless someone puts in real dice and a video camera. LOL
    And on that note, someone actually did that: Automatic Dice Machine Records 1.3 Million Rolls a Day
    Consider this post signed

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Nice! Made my day.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Add srand(time(NULL)); before you use rand(). That's how I learned it, anyway.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Thanks everybody, but one more question...

    I want to run a timer, but my current implementation makes the CPU go crazy:
    Code:
    int timeStart = time(NULL);
    while (1 == 1) {
    	if (timeStart + 5 <= time(NULL)) {
    		FUNCTION TO EXECUTE IN 5 SECONDS
    		break;
    	}
    }
    Is there a better way in C? (I'm fairly new to C if you can't tell...)

  8. #8
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    1) time() does NOT return an int
    2) So what happens if (timeStart + 5) is greater then time(NULL) ?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Depending on your compiler, you want either sleep(5); or delay(5). One will be for 5 seconds, and one will be for 5/1,000ths of a second.

    Code:
    #include <dos.h>
       
    
       while (1) {
            delay(5);   //pick just one
    	sleep(5);   //of these two lines
    	//FUNCTION TO EXECUTE IN 5 SECONDS
    	break;
       }

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    I'm using a Mac by the way, so I don't have access to dos.h, but I do have a wait() function that seems to be doing nothing...

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Any help from your compiler help or man pages?

    You'll need to have the right include file, or (a poor solution for sure), use a system call.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Maybe the wait function expects milliseconds... that is for every second, multiply by 1000. Just a thought. I really don't know at the moment but that's the way I remember it in BASIC.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Quote Originally Posted by nonoob View Post
    Maybe the wait function expects milliseconds... that is for every second, multiply by 1000. Just a thought. I really don't know at the moment but that's the way I remember it in BASIC.
    I've tried a value of 5000000 (it didn't work at all), and I'm not getting back anything from the compiler (whatever X-Code uses...)

    @Adak, yeah, system calls are my last resort. They just seem like a bad idea...

  14. #14
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Go to the terminal and type in "man 3 sleep" and read the entry there.

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Thanks so much rags_to_riches, this is exactly what i was looking for! the program is at devin.gotdns.com/downloads.html if anyone is interested

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 09:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 09:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 05:11 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