This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Generating a random bit - lack of randomness in C rand()
(4 answers)
Closed 3 years ago.
I am creating a program that prints out 1000 random occurrences of the letters "H" and "T" and my code implements rand()%2 but as i can see from running this program (code below) that its not completely random (the letters are random but always the same with every execution). I want to establish a more effective way by implementing RANDMAX to make every case of the program running completely random, how would I be able to do this?
#include <stdio.h>
#include<stdlib.h>
int main()
{
int i=1,n;
char ch ;
for( i = 1; i <= 1000 ; i ++ )
{
n = rand()%2;
if(n==0)
ch = 'T';
else
ch = 'H';
putchar(ch);
}
return 0;
}
Just add
srand(time(NULL));
after
int main()
{
That will initialize the random generate with a new seed every time you run the program. In this way, you'll get a new random sequence every time.
Related
This question already has answers here:
Non-static variable initialization
(7 answers)
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed last month.
I have typed out a simple program counting the number of positive divisors of a number in C, which works in CodeBlocks GCC compiler but not in the online compiler provided by the school, where it prints out some numbers close to 22k (22019 21873) etc after some inputs.
For example, for input 10 the program prints out 4 which the number of divisors, but in the online compiler it prints out 22019. I noticed when i changed the order of some conditions in the if statements from if (n<0) to if (0>n) the numbers also changed (the 22019 became 22xxx) to numbers different from the first time. Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, n, res = 0;
int i = 1;
scanf("%d", &n);
if (0>n)
printf("Wrong Input");
else {
while (n>=i)
{
res = n%i;
if (res == 0) {
c++;
i++;
}
else {
i++;
}
}
printf("%d", c);
}
return 0;
}
This question already has answers here:
Why does rand() yield the same sequence of numbers on every run?
(7 answers)
Closed 2 years ago.
My problem is really simple. In C, I am trying to create a set of random values to set for r, however whenever I run the code it generates the same numbers over and over again rather than a unique sequence of numbers on every iteration. How should I change the code to fix this?
My code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
int r;
for (int i;i<5;i++)
{
r=rand() % 10;
printf("%d\n",r);
}
}
This code always returns the values 1,7,4,0,9. How can I make it so that it instead randomizes each on every successive use of the function?
rand does not generate real random numbers. But to make the unique you need to seed it with something which will be different every time you run the program.
Example:
int main()
{
int r;
srand(time(NULL));
for (int i;i<5;i++)
{
r=rand() % 10;
printf("%d\n",r);
}
}
This question already has answers here:
How to generate a random int in C?
(32 answers)
Closed 6 years ago.
If I want to create a username generator in C? How do I go about it? I do understand that the question has been asked. But, I don't see anyone asking it in C language.
I think making it from a random word from the dictionary is going to be a little complicated for me to understand. I am right now only going to concentrate on a simple thing, and that is GENERATING 4 RANDOM NUMBERS. Now, before anyone says it's not secure or anything, this is purely for my curiosity and tinkering experience! I am enjoying learning C and I want to learn more.
The code that I have been able to come up with (minus the generator!) is as follows:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int randomnumbers; // this is for storing whatever random numbers I may generate
char NAME[15];
prinf("What is your first name? \n");//
scanf("%s \n", NAME);// I put it caps so that I can easily see it
printf("The username for %s is %s%d \n",NAME,NAME,randomnumbers);
return 0;
}
In case you find that the code is lacking and is of poor taste, I apologise. I am a newbie with less than a week in experience in coding. I thank you for your help and guidance.
rand is what you need.
int randomnumbers = rand();
You may also want to read on how to seed the random number generator
Bonus random username generator:
// buffer must have memory for len + 1 elements
void generateUserName(/*out*/ char *buffer, int len)
{
srand(time(NULL));
int idx = 0;
while (idx < len) buffer[idx++] = rand() % 26 + 'a';
buffer[len] = '\0';
}
To be called as
char userName[10];
generateUserName(userName, sizeof(userName) - 1);
printf("%s\n", userName);
This question already has answers here:
How to generate a random int in C?
(32 answers)
Closed 8 years ago.
I'm writing a code that will return a random number between 5 and 20, and i'm running into a problem where it will always produce the same number over again and i can't seem to solve it.
#include <stdlib.h>
#include <stdio.h>
int random = 0;
int randomnumbergen() {
srand(12345);
random = rand() % (20 - 15) + 15;
return random;
}
First, you'll want to call srand() at the beginning of your program one time.
Next, you'll want to replace
srand(12345);
with
srand (time(NULL));
You're using the same seed each time - producing identical results. You need to not hardcode it.
This question already has answers here:
Array index out of bound behavior
(10 answers)
Closed 8 years ago.
I was trying to test what output I would get when I tried to print the array that the index goes out of bound.
The code:
#include <stdio.h>
void main()
{
int arr[] = { 3, 4, 5 };
for (int i = 0; i < 5; i++)
{
if (arr[i] == 0)
{
printf("Breaking out of the loop.");
break;
}
else
printf("%i\n", arr[i]);
}
getchar();
}
When I run this code, the output is:
3
4
5
-858993460
3997200
I expected it to print out "Breaking out of the loop" and break out of the loop and terminate.
I truly have no idea how it even printed those numbers.
Any idea what those numbers mean?
P.S. I am sorry if this is a stupid question, I am quite new to C.
Memory out of bounds of an array, or dynamically allocated memory, doesn't belong to you, and its content is indeterminate. Accessing arrays or memory out of bounds leads to undefined behavior. Just don't do it.