Random selection

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Random selection

 
0
  #1
Nov 26th, 2008
i have an array of 16 things, now 8 of them have to be chosen at random so basically what i want to do is

random(CHR/2)

like choose a random number from CHR/2 (as there are 8 not 16 now)

what code would i use because that doestn work
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Random selection

 
0
  #2
Nov 26th, 2008
what is "CHR/2" ? If you need 8 random numbers between the values of 0 and 16, then put them in an array.
  1. start loop
  2. generate random number between 0 and 16
  3. first time the number has been generated ?
  4. no, then go back and generate another number
  5. yes, add to an array of generated numbers
  6. if 8 numbers have been generated than exit loop
  7. end of loop

to generate a number between 0 and 15 int x = rand() % 16
Last edited by Ancient Dragon; Nov 26th, 2008 at 9:51 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 61
Reputation: AceofSpades19 is on a distinguished road 
Solved Threads: 10
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Junior Poster in Training

Re: Random selection

 
0
  #3
Nov 27th, 2008
Originally Posted by Ancient Dragon View Post
what is "CHR/2" ? If you need 8 random numbers between the values of 0 and 16, then put them in an array.
  1. start loop
  2. generate random number between 0 and 16
  3. first time the number has been generated ?
  4. no, then go back and generate another number
  5. yes, add to an array of generated numbers
  6. if 8 numbers have been generated than exit loop
  7. end of loop

to generate a number between 0 and 15 int x = rand() % 16
rand() is in cstdlib and to make it more random you should seed it first with srand()
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Random selection

 
0
  #4
Nov 27th, 2008
to seed the random number with srand the *best* method is as follow.

  1. #include <ctime>
  2. ...
  3.  
  4. srand(time(NULL));

Note you only need to seed rand once in your program.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC