Thread: Random Number Generation-Increments

  1. #1
    Registered User
    Join Date
    Mar 2010

    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

    #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.

    int getspin()
    	int x; 
    	x = (rand()%1001 +50)/100 *50; 
    	return x; 

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You may find it easier to generate numbers in the range [1,20], and then multiply by 50.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    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, 10:13 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05: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
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM