Need a random question answered (I mean for making random numbers ;)

This is a discussion on Need a random question answered (I mean for making random numbers ;) within the C Programming forums, part of the General Programming Boards category; Okay, my first question is how do you get a random number that will either be a 1 or a ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    132

    Need a random question answered (I mean for making random numbers ;)

    Okay, my first question is how do you get a random number that will either be a 1 or a 0. I can't figure out how to do that using srand() and rand(). I can get all 0's or all 1's but not random between the two.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Like a lot of things, this is covered very well in the FAQ. Look at the third example, it generates numbers between whatever you ask (in this case 10 and 12), what about if you told it to get numbers between 0 and 1? Take a look at how it works.


    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    int main()
    {
    	srand((unsigned)time(0));
    	int what_to_do = rand()%3; // generates a random number between 1 - 3
    	if(what_to_do == 1)
    	{
    		// do something
    	}
    	else if(what_to_do == 2)
    	{
    		// do something
    	}
    	return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C board = C code.
    C++ board = C++ code.

    See the connection?

    Now to your incorrect code:
    Code:
    int what_to_do = rand()%3; // generates a random number between 1 - 3
    No it doesn't. It makes a random number between 0 and 2. And why do you have three choices, when you only use two of them? There's a 33% chance your program will do nothing when it runs.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    132
    Thanks for the help guys. Can't believe I never figured that simple thing out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 PM
  2. calculating the variance of random numbers
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 11-22-2004, 07:16 AM
  3. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  4. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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