Thread: rand()

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    rand()

    How can i generate 128 random numbers on an interval of -64 to 64?

    thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Generate a random number (integer?) in the interval [0, 128], and repeat another 127 times. To make each number be in the range [-64, 64], just subtract 64 from each number generated.
    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
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    perfect thanks...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is it ok to have repeats, or must the numbers all be unique?

    If they all have to be unique, then what you want to do is create an array with all the numbers in it (there are 129 numbers between and including -64 and 64, so you'd have to have an array of size 129). Then randomly shuffle the array. The best and easiest way to do that is to call the random_shuffle algorithm. If you aren't allowed to use that because of class requirements or something, you can mimic the algorithm. Be careful, though, because it is easy to get the algorithm slightly wrong and really throw off the random distribution.

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