rand()

This is a discussion on rand() within the C++ Programming forums, part of the General Programming Boards category; How can i generate 128 random numbers on an interval of -64 to 64? thanks....

  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
    21,588
    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.
    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
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    perfect thanks...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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, 12: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, 12:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 09:13 PM

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