Random numbers

This is a discussion on Random numbers within the C Programming forums, part of the General Programming Boards category; Hey all, Im trying to produce random numbers in the range of 1 to 50,000 using the rand() function. just ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Question Random numbers

    Hey all,

    Im trying to produce random numbers in the range of 1 to 50,000 using the rand() function.

    just a little curiouse as to how this function works.

    Ive been fiddling with it and i havent learnt much. Im guessing the seed you enter should influence the numbers generated, but ive been entering in new seeds to find that the numbers generated are exactly the same. This could have something to do with the complexity of my seeds however..

    i tried numbrs like 7, 50.. then randomly mashing the number pad. but it didnt work.

    if i don't use a seed but a formulae such as:

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

    it will generate what seems to be random but each time it is called.. each time the program is run the pattern is exactly the same.

    I tried entering a seed into the rand function here but it didnt seems to make any difference.

    Also one last thing. This formula is supposed to generate numbers betweek 1 and 10. but it doesnt.

    thanks,
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This should work for you.
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <time.h>
    
    int main ( void )
    {
      int i = 0;
      srand ( time ( NULL ) );
      while ( i < 50 )
        printf ( "%u\n", 1 + rand() % 50000 ), i++;
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    thanks

    hey thanks,

    I didnt know about the srand function.

    now that i know about that it works fine.

    If I do it the way your suggest however it will use low ordered bits. I need to use high ordered.

    Thanks all the same.

    cheers,
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Additionally, you could always READ THE FAQ.

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

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    This makes your "random" numbers more random:
    Code:
    rand()*rand()*rand()%MAX;
    klausi
    When I close my eyes nobody can see me...

  6. #6
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Hi,
    I didnt understand this statement

    printf ( "%u\n", 1 + rand() % 50000 ), i++;


    Could u please explain it in detail to me?


  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    Hi,
    Be aware that different compilers have different values for RAND_MAX.

    Thus the formula

    x = rand() % 50000 + 1

    may not give a vaule higher than 32,768.
    Last edited by Pappy1942; 03-15-2002 at 08:29 AM.
    Pappy
    You learn something new everyday.

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, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06: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, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21