It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is there any way to generate a random number in C without using seed.
Here what have so far but it still using srand(time(NULL)); which is a seed.
#include <stdio.h>
#include <time.h>
#include <math.h> /* required for sqrt() */
#include <stdlib.h> /* required for rand() */
int gen_rand(); /* note these are declarations of functions */
void main()
{
int number;
srand (time(NULL)); /* everytime you run program, it will give you different result */
number = gen_rand();
printf("%d is the power of 2 of %.0lf\n", number, sqrt(number));
}
/* Function generates random number power 2 of 20 - 230 */
int gen_rand()
{
int n;
n = rand() % 211; /* n is random number in range of 0 - 210 */
n = n + 20; /* n is now in range of 20 - 230 */
return(n*n); /* return n to the power of 2 */
}
Yes and no.
There are basically two methods to get even remotely random numbers in c.
1) have a pseudo random number generator with seed -- that is an algorithm, that produces some sequence of numbers using clever arithmetic operators and possible lots of internal variables that are mixed, permuted, twisted and whatever. The seed can be implicit (i.e. always zero and each time you run the program, the same sequence is generated). Or it can be explicit, where the seed can be changed somehow between runs.
2) Using external source of data, that somehow changes in between runs. That could come from a timer, environment variables (program id perhaps), noise from camera, mouse movements etc.
1+2) use the external source of noise as seed to pseudo random number generator.
All non-hardware based PRNG require some form of random input to combat their deterministic nature, thus a seed will always be required.
You can try abuse /dev/rand under linux (but it also is a PRNG), or if you have a very modern Intel CPU, their new digital RNG facilities would work.
No. If you don't seed the automatic number generator, it will behave deterministically and yield the same numbers every time.
Yes. Generating random numbers by using the rand() function without using a seed will give you the same set of random numbers though.
Related
This question already has answers here:
How does srand relate to rand function?
(4 answers)
Closed 7 years ago.
I am generating random numbers , and someone told me to use srand. I searched about srand and found it is used on top of rand(). But I do not understand why not use just rand() since I get random numbers in my below code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int i=0;
for(i=0;i<5;i++){
printf("random number is %d\n",rand());
}
}
And I get a set of 5 random numbers. So rand() does gives random numbers. Why do we need srand on top of that then?
If you simply use rand() and run your code multiple times you will notice that you tend to get the same sequence of random numbers every time.
srand is used to seed the random number generator. This allows you to generate different sequences. I'd suggest that you read the manual page for srand.
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 7 years ago.
Using rand(), I'm trying to create an array which generates 32 random numbers each time I run the program. However my program below gives me the same 32 random bits each time. Is there anyway I can get it to generate different 32 bits each time I run the program?
for(a=0;a<32;a++)
{
ran[a]= (rand()%2);
}
You need to set up the random seed each time you run the program to something different to do that. What people do is usually this:
#include <time.h> /* time */
...
srand (time(NULL));
...
//Calls to rand();
Then your random seed is different at every start of the program. Check more details about srand here.
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.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
The exercise asks to find which of the numbers from 1 to 500, the sum of the numbers specific digits, raised to the third power equals that particular number.
for example 1^3=1
and 371 makes 3^3+7^3+1^3 = 371
How I approached the problem:
I was thinking if I could have an array of strings with 500 slots, each slot containing a string converted number, then I could do math with each slot's string. If they met the criteria I would apply then that slot would be printed.
I tried the function sprintf without much success. In a loop it just initializes the strings (or is it arrays? after 3 hours I am confused) [0] slot, leaving all other slots unchanged.
I don't want you to solve the exercise, rather than guide me with my logic. Please ask me to add code of what I did if you want to.
Always start by clearly defining your algorithm, so you know what you are doing. Split it up into simple steps. Something like this:
For each integer i in the interval 1 to 500:
Check if the condition holds for this i
If it holds:
Print i
else:
Do nothing
Now you need to define "Check if the condition holds for this i". I would use some modulo and division arithmetics to extract the digits, but I leave the details to you.
Note that I have talked nothing about C or any other programming language. Only when you know your algorithm should you start thinking about implementation.
(There is actually the possibility of a slightly different algorithm than the one given above, where you have one loop for each digit nested inside each other. That solution may be acceptable to you but it will not be as generic)
for(i=1;i<=500;i++)
{
//loop for checking each number i
int sum=0; // to store the sum of cube of digits
int n=i; //copy of i
//The below while loops does the task. It extracts a digit from the number and adds its cube to the sum
// last digit from the number can be seen by taking its remainder by 10 . For eg 35%10=5
//once we have used this digit make the number shorter by dividing by 10. For eg 35/10 becomes 3 (because of integer divisions)
while(n>0)
{
int rem=n%10; //extract the last digit
sum+=cube(rem); //cube function raises a number to its cube
n/=10; //remove the digit we had extracted earlier from the number
}
if(sum==i) //we got the number we wanted
printf("%d\n",i);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Most modern computers exhibit non deterministic behavior, what makes impossible to tell how many clock cycles will occur between two consecutive calls to read the computer clock.
The following code is a pseudo random number generator for one byte using the computer clock.
unsigned long occurrences = 0;
unsigned long total = 0;
while (true) {
if ((clock() & 0xFF) == 60) // testing ocurrences for a given number, 60 for instance
occurrences++;
total++;
printf("%f\n", (float)occurrences / (float)total ); // this should be approximately 1/256 = 0.00390625
}
Excluding serious applications like encription for instance, it could be used in mobile platforms for games.
I wonder what could be the advantages and disadvantages of such implementation.
You are missing the proper way to use rand() or, more specifically, srand().
You need to call srand() exactly once during program run.
Do not call srand() in a loop.
Do not call srand() before each call to rand().
The best way to ensure proper srand() management is to call it once inside your main() function and then forget about it: just use rand() afterwards.
#include <stdlib.h> /* rand, srand */
#include <time.h> /* time */
int main(void) {
/* initialization */
srand(time(NULL));
/* input, possibly calling rand() */
/* process, possibly calling rand() */
/* output, possibly calling rand() */
/* termination */
return 0;
}
You should do the "splitting" using a union instead of pointers like that.
And i agree that random numbers and clock are two completely different things, and you made more of a statement than asking a question.