I want to generate a random number/character without using any libraries but <stdio.h>. Is there any possibility to do that? I mean, by doing some weird loop or something like that, I don't care, I just want to generate random stuff with the basic library.
Asked
Active
Viewed 91 times
-6
-
2Yes, you can write a random generator from scratch. You don't even need the standard library to do that. – klutt Jan 19 '22 at 12:54
-
2Google "pseudo random number generator algorithm" – Jabberwocky Jan 19 '22 at 12:54
-
3`stdio.h` is not a library. It is a header. It is important to understand the difference. – William Pursell Jan 19 '22 at 12:56
-
1There is a decent example at https://en.wikipedia.org/wiki/Linear-feedback_shift_register – William Pursell Jan 19 '22 at 12:57
-
You will definitely want to `#include
` too to get the fixed width `typedef`s like `uint32_t` etc. Then pick a good PRNG algorithm and implement it. This has a lot of interesting stuff: http://mostlymangling.blogspot.com/ – Ted Lyngmo Jan 19 '22 at 13:01 -
1It would be cheating, but if you're allowed to use functions in `
`, you can open and read from `/dev/random`... – Steve Summit Jan 19 '22 at 13:03 -
Does this answer your question? [Generating random numbers in C](https://stackoverflow.com/questions/3067364/generating-random-numbers-in-c) – lbarqueira Jan 19 '22 at 13:06
-
1@lbarqueira no, it doesn't, because he does not want to use other functions than the ones from `
` – Jabberwocky Jan 19 '22 at 13:25 -
You could use the code outlined in the C standard for a minimal [pseudo-random number generator](http://port70.net/~nsz/c/c11/n1570.html#7.22.2.2p5). It isn't clear whether you're allowed to use functions of your own, or whether the only functions other than `main()` can be those declared in the `
` header. Also, it isn't clear if your program must output a random number when it is invoked, or whether there is other code in the program that will use it. – Jonathan Leffler Jan 19 '22 at 14:11
1 Answers
-2
#include <stdio.h>
int main()
{
int *f, e = 3;
f = &e;
printf("%d", f);
return 0;
}
its not realy random but the best you can do with stdio.h
you have a pointer and the pointer points on a variable named 'e'. and then you print the memory adress of the pointer

zlSxrtig
- 71
- 6
-
1[Dilbert](https://dilbert.com/strip/2001-10-25) found a random number generator like this. – Jonathan Leffler Jan 19 '22 at 14:29
-
1
-