943,550 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1389
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 24th, 2009
0

C++ random between two choices ?

Expand Post »
Can some help me with a random between two choices ?

Something like:

L = random(A, B);

and in L to have A or B.

Thx.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johndoe123 is offline Offline
2 posts
since Sep 2009
Sep 24th, 2009
1

Re: C++ random between two choices ?

Here's how to do it:

C++ Syntax (Toggle Plain Text)
  1. int choices[2] = {2,3}; //choose between 2 and 3
  2. // Seed the random generator with srand()
  3. // Use rand() to pick either 0 or 1 and store in variable x
  4. int L = choices[x];

Google for srand() and rand() to find out how they work, or read this
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 24th, 2009
0

Re: C++ random between two choices ?

or u can do it as

C++ Syntax (Toggle Plain Text)
  1. int random(int a, int b)
  2. {
  3. srand(time(NULL));
  4.  
  5. int r = rand()%2;
  6.  
  7. if(r==0)
  8. return a;
  9. else
  10. return b;
  11. }
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Sep 24th, 2009
0

Re: C++ random between two choices ?

if random is used like random(high number)
why not do random(number)+15
Reputation Points: 10
Solved Threads: 0
Light Poster
maxicube is offline Offline
32 posts
since Nov 2008
Sep 24th, 2009
0

Re: C++ random between two choices ?

Click to Expand / Collapse  Quote originally posted by maxicube ...
if random is used like random(high number)
why not do random(number)+15
sorry but i didn't get your question. Can u put some light on what u wanted to know.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Sep 24th, 2009
0

Re: C++ random between two choices ?

so if 0 is randoms starting number, you add 15 so... 15 is the minimum number
Reputation Points: 10
Solved Threads: 0
Light Poster
maxicube is offline Offline
32 posts
since Nov 2008
Sep 24th, 2009
0

Re: C++ random between two choices ?

Click to Expand / Collapse  Quote originally posted by maxicube ...
so if 0 is randoms starting number, you add 15 so... 15 is the minimum number
offcourse..........

u can manipulate the random number in whatever way u want it.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Sep 24th, 2009
0

Re: C++ random between two choices ?

thx niek_e, but is there any other way how i to make this with a single line without to use much variables ? Idea is to pick one of these two chars A or B.

Like

char L = random(from char A, or char B);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johndoe123 is offline Offline
2 posts
since Sep 2009
Sep 24th, 2009
0

Re: C++ random between two choices ?

Click to Expand / Collapse  Quote originally posted by johndoe123 ...
thx niek_e, but is there any other way how i to make this with a single line without to use much variables ? Idea is to pick one of these two chars A or B.

Like

char L = random(from char A, or char B);
u can use the function that i gave as

C++ Syntax (Toggle Plain Text)
  1. char x = random('A', 'B');
just change its prototype to
C++ Syntax (Toggle Plain Text)
  1. char random(char a, char b);

i hope thats simple enough. i guess thats simplest way to do it.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Sep 24th, 2009
0

Re: C++ random between two choices ?

hi there i'm new to this and I have a problem with dreamweaver and don't know who to ask.
when I try to view my pages with hitting F12 it keeps saying that I have a broken link and a DNS error.
can you point me in the right direction?
Thank you in advance
Robert
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dunnotheclown is offline Offline
1 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Number of Variables in Constructor
Next Thread in C++ Forum Timeline: LINQ with C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC