iam trying to make a random number in a loop but the same answer keeps on coming back... my code is rand()%4.how do i make it change randomly in every loop???what specific code should i add?
How do you make random numbers in C program?
1 Generate random number
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
int main(void)
{
int magic; /* magic number */
int guess; /* user's guess */
magic = rand(); /* generate the magic number */
printf("Guess the magic number: ");
scanf("%d", %26amp;guess);
if(guess == magic)
printf("** Right **");
return 0;
}
2 A random number is chosen between 1 and 100
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
int main()
{
int number_to_guess = rand() % 100 + 1;
printf("%d ",number_to_guess);
}
3 Generate random number: how to use rand
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;time.h%26gt;
int main ()
{
/* initialize random generator */
srand ( time(NULL) );
/* generate random numbers */
printf("RAND_MAX = ", RAND_MAX);
printf ("A number between 0 and RAND_MAX : %d\n", rand());
printf ("A number between 0 and 99: %d\n", rand() % 100);
printf ("A number between 0 and 9: %d\n", rand() % 10);
return 0;
}
Reply:I think you have to learn proper as you are on this stage you have to improve yourself
i have given a way to do this but you need more help from me
then i will sure help you
just work with a book of c pro...
here r many
http://www.free-ebooks-junction.com/
Reply:Random number generators are almost always pseudo-random number generators, and given the same seed, will produce the same sequence of "random" numbers. If it weren't this way, programs that rely on them would be maddeningly difficult to test!
The way around it is to either spend some (considerable) effort to come up with a random seed, or start the sequence in some random place. Either way can take a bit of computation, but once you have gotten the random number generator started in a random place, you can treat the results of rand() as random enough for most applications.
There are a number of ways to do this. One of the best is to use something done by the user, for example, measuring the time between keystrokes or mouse clicks, providing you can do this with enough precision. If your application is mouse-driven, for example, you could be continuously calling rand() while waiting for the user to click the mouse.
Depending on your environment, you might have access to some peripheral, say an analog input, that has random noise. This can sometimes be close to truly random.
You can come close, close enough for things like games, by looking at the time. Take, say, the milliseconds portion of the time and use that as a starting place.
At the end of the day, truly random is very hard to come by, but "random enough" usually isn't all that bad if you give it a bit of thought.
Reply:Make sure you are assigning the generated random number in each loop to some variable..........example...... y=rand(range);
Reply:Have you tried calling srand(10000) before your loop?
Reply:you are trying to make it an integer with the %4? You are trying to make it into a whole number?
if that is the case you are going to need to do something to the rand() before you make it to a integer. Random numbers in code is a number between 1 and 0 ... so it will always be a decimal number. What you need to do is multiple the rand() by some power of 10 to bring it some value to the correct side of the decimal so when you turn it into an interger it will work.
Reply:http://www.cprogramming.com/tutorial/ran...
Google is your friend.
Reply:u must include the header %26lt;stdlib.h%26gt; , then in your main function, call randomize();
then u can use it, be calling the random function.ex : a =random(100);
it will random 100integers from 0 to 99, then it assigns to a..
Reply:theres a method you can use:
int rand(void);
rand() returns successive pseudo-random numbers in the range from 0 to (2^15)-1.
include all the header file though, %26lt;stdlib.h%26gt;
song downloads
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment