Random Number Generation-Increments

This is a discussion on Random Number Generation-Increments within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to generate a random number between 50 and 1000(inclusive) in increment of 50. I'm using the libraries ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    15

    Random Number Generation-Increments

    Hi, I'm trying to generate a random number between 50 and 1000(inclusive) in increment of 50.

    I'm using the libraries

    Code:
    #include <ctime>
    #include <cstdlb>
    Here's the actually code I'm using.... I'm calling this function inside of a loop in the main function. I'm getting the correct numbers but I am occasionally getting 0 and the same number often repeats itself.

    Code:
    int getspin()
    {
    
    	srand(time(NULL));
    	int x; 
    	x = (rand()%1001 +50)/100 *50; 
    
    	return x; 
    
     }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    You may find it easier to generate numbers in the range [1,20], and then multiply by 50.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The range of what you have is anywhere from 0 to 500, it would look like. And if you couldn't get repeats, it wouldn't be a random number, would it?

    There are twenty possible outcomes. It's easy to get rand between 0 and 19; can you think of a way to do
    0 -> 50
    1 -> 100
    2 -> 150
    3 -> 200
    .
    .
    .
    18 -> 950
    19 -> 1000?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  5. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 04:35 AM

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