Thread: rand()

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Question rand()

    I have written a simple little math game. I only seed the srand() once but out of 25 uses of rand(), I must get 3 or 4 numbers come back 3 or 4 times.

    Is there a way to make sure numbers are not duplicated when using the rand(). It doesn't seem so random

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    If u want no duplicates, u can use an array to store numbers generated and if the newly generated number matches any of the numbers in the array then the program can generate a new random number and test it again.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    Is there a way to make sure numbers are not duplicated when using the rand(). It doesn't seem so random
    <<<

    Of course, if you do this, it is also not random! Maybe you've gone wrong somewhere. Post your code.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Rand shouldn't be turning up so many duplicates... for example, the following code
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <limits.h>
    int main ()
    {
     int i;
     srand(0);
     for (i = 0; i < 20; i++) printf ("%d\n", rand());
     printf ("\n\n");
    
     return 0;
    }
    Produces no duplicates.
    Code:
    0
    262236772
    208042609
    800207362
    428428949
    47718728
    1793958355
    1543227091
    1255063701
    1245922717
    448084510
    1841298076
    1050803247
    489249763
    634882132
    437249775
    192858938
    1769723075
    2017244079
    451954759
    There's probably something in your code forcing the numbers to be the same.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Unless you're just trying to produce random numbers over a relatively smalle interval (say, 1-20 or so), in which case duplicates are inevitable...

  6. #6
    Unregistered
    Guest

    Post

    here is the code, i only need 40 random numbers. all ideas welcome. i still have a couple things left in to test with

    /*********************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    main() {

    /*** VARIABLE DECLARATIONS ***/
    int i,f,s,r;
    int answer = -99;
    int correct = 0;
    int wrong = 0;

    /*** ARRAY DECLARATIONS ***/
    int problem[169] [1] ;

    /*** initialize variables ***/
    f = 0 ; /* first number in equation */
    s = 0 ;

    system("cls");

    for (i=0; i < 169 ; i++)
    {
    if ( i > -1 && i < 13)
    f = 0;
    else if ( i > 12 && i < 26)
    f = 1;
    else if ( i > 25 && i < 39)
    f = 2;
    else if ( i > 38 && i < 52)
    f = 3;
    else if ( i > 51 && i < 65)
    f = 4;
    else if ( i > 64 && i < 78)
    f = 5;
    else if ( i > 77 && i < 91)
    f = 6;
    else if ( i > 90 && i < 104)
    f = 7;
    else if ( i > 103 && i < 117)
    f = 8;
    else if ( i > 116 && i < 130)
    f = 9;
    else if ( i > 129 && i < 143)
    f = 10;
    else if ( i > 142 && i < 156)
    f = 11;
    else if ( i > 155 && i < 169)
    f = 12;

    problem[i][0] = f ;

    if ((s < 0) || (s > 12))
    s=0;

    problem[i][1] = s ;
    ++s;
    }/*** end of filling up the array ***/

    srand(time(NULL));
    do
    {
    r = rand() / (RAND_MAX / 169 + 1) ;
    printf("%d X %d = ", problem[r][0], problem[r][1]);
    scanf("%d", &answer);
    if (answer == (problem[r][0] * problem[r][1]))
    {
    system("cls");
    ++correct;
    switch (correct)
    {
    case 1 :
    printf("Your starting to get warm\n");
    break;
    case 2 :
    printf("Don't make it look to easy\n");
    break;
    case 3 :
    printf("Now your starting to cook with gas\n");
    break;
    case 4 :
    printf("I think you realy got the hang of this now\n");
    break;
    case 5 :
    printf("Keep it up, you doing fine\n");
    break;
    case 6 :
    printf("Ride on pardner\n");
    break;
    case 7 :
    printf("Your outstanding\n");
    break;
    case 8 :
    printf("GRRREEEEAAAATTTT JJJJOOOOOBBBB\n");
    break;
    case 9 :
    printf("Fantastic\n");
    break;
    case 10:
    printf("The force is with you at this half way point\n");
    break;
    case 11:
    printf("Outstanding\n");
    break;
    case 12:
    printf("Your so good\n");
    break;
    case 13:
    printf("You really know your math\n");
    break;
    case 14:
    printf("Happy days\n");
    break;
    case 15:
    printf("Good job\n");
    break;
    case 16:
    printf("You rally are smart\n");
    break;
    case 17:
    printf("Scorching\n");
    break;
    case 18:
    printf("I'm scared, your to good\n");
    break;
    case 19:
    printf("Just one more like this one genuis\n");
    break;
    case 20:
    printf(" CONGRATULATIONS\n\n");
    printf("\tooo o ooo\n");
    printf("\to o o o o\n");
    printf("\tooo o ooo\n\n");
    printf("\to o\n");
    printf("\t o o\n");
    printf("\t o o\n");
    printf("\t o o\n");
    printf("\t o o\n");
    printf("\t o o\n");
    printf("\t o o\n");
    printf("\t o o\n");
    printf("\t o\n");
    break;
    default :
    printf ( " help i'm broken and can't get fixed");
    break;
    }
    }
    else
    {
    system("cls");
    ++wrong ;
    switch (wrong)
    {
    case 1:
    printf ("Stop and think about it\n");
    break;
    case 2:
    printf ("Does your one brain cell get lonely\n");
    break;
    case 3:
    printf ("Help your brain has fallen and it can't get up\n");
    break;
    case 4:
    printf ("My grandma can do math better than you\n");
    break;
    case 5:
    printf ("It's really starting to smell in here\n");
    break;
    case 6:
    printf ("Your good at math, (NOT)\n");
    break;
    case 7:
    printf ("Sorry try again\n");
    break;
    case 8:
    printf ("Look, a fireball crashing to earth\n");
    break;
    case 9:
    printf ("Lost again I see\n");
    break;
    case 10:
    printf ("Your hurting now\n");
    break;
    case 11:
    printf ("Does your mama do your homework for you\n");
    break;
    case 12:
    printf ("Take your shoes off, maybe it will help\n");
    break;
    case 13:
    printf ("I'll bet you can't even remember your name\n");
    break;
    case 14:
    printf ("Go and get some more schooling\n");
    break;
    case 15:
    printf ("Do you need some extra fingers or toes\n");
    break;
    case 16:
    printf ("How does it feel to be wrong this much\n");
    break;
    case 17:
    printf ("Look ma, no brains\n");
    break;
    case 18:
    printf ("One foot in the grave now\n");
    break;
    case 19:
    printf ("Your in second grade right\n");
    break;
    case 20:
    printf ("\t\t\tI WIN\n\n\n\n");
    break;
    default:
    printf ("HELP I'M BROKEN");
    break;
    }
    }
    }while ((correct != 20) && (wrong != 20));

    return;
    }

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Red face

    oops the post above is mine , i forgot to log in.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    try something like this:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        int array[21] = { NULL };
        int i, num;
    
        srand((unsigned)time(NULL));
    
        for( i = 0; i < 20; ++i)
        {
            do
            {
                num = (rand() % 20) + 1;
            }
            while(array[num]);
    
            array[num] = 1;
    
            printf("%d ", num);
        }
        return 0;
    }
    the random number is used as an index for an array of int. when rand generates a number, that element of the array is set to 1 so that if the same number is generated again, the do-while will keep looping until rand() returns a new number.

    hope this helps.

    hmm, nutshell pretty much gave the same answer. oh, well. my reply can be an example to back it up.
    Last edited by CtrlAltKick; 02-12-2002 at 12:18 PM.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Talking

    Thanks all, got it now. I changed the code and got rid of the arrays all together.

    srand ((unsigned)time(NULL));
    do
    {
    f = (rand() %12) + 1 ;
    s = (rand() %12) + 1 ;
    printf("\n%d X %d = ", f, s);
    scanf("%d", &a);
    if (a == f * s)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM