Generating ranged randomized numbers

This is a discussion on Generating ranged randomized numbers within the C++ Programming forums, part of the General Programming Boards category; Can someone provide me with a string that generates randomized numbers that have a range from #-#. So if i ...

  1. #1
    C++ Master
    Join Date
    Oct 2005
    Location
    ProgramLand
    Posts
    15

    Generating ranged randomized numbers

    Can someone provide me with a string that generates randomized numbers that have a range from #-#. So if i wanted the DOS window to generate a number from 5-10; how would i do it? Thx
    -Cloud

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Can someone provide me with a string that generates randomized numbers
    What does that mean? How does a string generate random numbers?

  4. #4
    C++ Master
    Join Date
    Oct 2005
    Location
    ProgramLand
    Posts
    15
    i mean i want a code to generate me random numbers from range to range

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    That is in the link treenef posted.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    have a look at the boost random library.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    C++ Master
    Join Date
    Oct 2005
    Location
    ProgramLand
    Posts
    15
    Code:
    /*
     * This code is written in C, but it could just as easily be done in C++.
     * The rand() and srand() functions are available in both languages.
     */
     
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <time.h>  
    
    int main(void)
    {
      int i;
      
      srand(time(NULL));
      
      i = rand();
      
      printf ("Your random number is %d\n", i);  
    
      printf ("This compiler can generate random numbers from 0 to %d\n", RAND_MAX);
    
      return(0);
    }
    How do i set the max?

  8. #8
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Look down further.
    Not only does it have that code for C++ but below that it also had the range code.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Also see Prelude's "Brain Vomit": http://www.eternallyconfuzzled.com/articles/rand.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. help with generating numbers
    By kurz7 in forum C Programming
    Replies: 8
    Last Post: 08-06-2003, 08:29 AM
  4. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 AM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM

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