English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello,
In c++, I want to create one real random variable(x) that is between zero and one (0 Thank you
Best Regards

2006-09-21 00:09:21 · 6 answers · asked by mechanical engineer 2 in Computers & Internet Programming & Design

6 answers

In c++ you can create a random number using random(n) function. But it only creates between 0 and n. For example if you want to create 3 digit real number x (0 x=(random(999)+1)/1000;
or if you want to create 6 digits real number then
x=(random(99999)+1)/1000000;
and so on...

That's all.
Good Luck...

2006-09-21 04:19:24 · answer #1 · answered by Lunatic T 2 · 0 0

From what I understand there is no way to get a true random number. The random functions have an ordered method to them, so inherently they will not produce a random number. But for all practical purposes the random function will work. The casino machine programmers are always struggling with getting the ultimate random number generator.

2006-09-21 08:42:24 · answer #2 · answered by luke7785 2 · 0 0

apparently the best random generator method up to date is the marsenne twister method. tried to look at it once, but it understanding it was beyond my capacity. That's beyond the scope of this question though. I'm pretty sure rand() would work. And if u use the time as ur seed, u'll likely have a relatively random number.

2006-09-21 22:19:02 · answer #3 · answered by rice kid 4 · 0 0

It depends upon the intended use and kind of random number you want. Are you looking for a uniform, gaussian, or some other distribution? The standard rand() and random() functions are generally sufficient for games but not for research (their quality as pseudo-random number generators is quite poor).

Do you really want the number to be in the range (0,1)? Most applications want the range [0,1) or [0,1].

2006-09-22 12:46:39 · answer #4 · answered by Jeff Alexander 2 · 0 0

The above answer produces nonsense for me.

You should call the function random(), or if your library doesn't have it, rand(). Don't forget to change the random seed each time you start your program, or you'll get the same random numbers over and over again.

2006-09-21 07:56:45 · answer #5 · answered by TGV 1 · 0 0

Here is your link

http://www.google.co.uk/search?hl=en&q=c%2B%2B+create+one+real+random+variable%28x%29+that+is+between+zero+and+one&btnG=Search&meta=cr%3DcountryUK%7CcountryGB

2006-09-21 07:22:15 · answer #6 · answered by Joe_Young 6 · 0 1

fedest.com, questions and answers