Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to generate random Add in visual C + +
But the rand and srand function does not produce really good numbers and distribution have not produced the numbers are the same numbers in different performances.
I'm working on a video project that forms part of opengl should be distributed randomly in space gradual've written., But I just can not so random numbers.
Now you have found a way to generate a random number, right?
Is there a way to generate a random number in C # I use the c? Like a header file?
I am grateful for your guidance
Posted

1 solution

Just like C, random number sequences are dependant on the the starting value - the seed - so if you create two sequences with the same seed they will be identical. Mostly, seeds are loaded from the system clock, so if you create two separate random instances at the same time, you will get the same sequence, even if you later use them an hour apart.

Look at your code: the chances are that you are using rand and srand badly and this is causing your problem. This[^] may help.
If you are using C#, as you suggest at the end of your question (or CLI C++) then you can use the Random class[^] from .NET, but in this case you should use and initialise the Random instance as a class member, not create it in a method / function as that will create the same problem as the srand mentioned above.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900