| |

VerySource

 Forgot password?
 Register
Search
View: 787|Reply: 2

Why does nRNum = rand () / RAND_MAX return zero?

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-10 17:20:01
| Show all posts |Read mode
double nRNum;
srand ((unsigned) time (NULL));
nRNum = rand () / RAND_MAX;
return nRNum;

Why are the returned values ​​all zero, not random numbers uniformly distributed in the interval (0,1), how can I generate random numbers in the interval (0,1)?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-13 15:00:01
| Show all posts
Both are int, and the division is naturally 0.


nRNum = rand () / (double) RAND_MAX; // Convert one of them
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-1-14 17:45:01
| Show all posts
Thank you so much, I just do n’t know if it is a (0, 1) interval pseudo-random number
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list