Computer Newbies forum

General discussion

rand function not random

by aliasnikhil / May 27, 2009 7:22 PM PDT

Hey guys,
i was tying some simple programs in c++ when i came across rand function in two programs.it should generate a totally new number every time.but my complier,which is ,DEV C++,generates the number 41 everytime.i tried restarting the complier but it did the same.
also my complier highlights all known functions but this time it did not highlight rand().I had included the <cstdlib> header.also when the output is to be generated i have to ALWAYS include the command cin.get() to keep the command prompt window open
PLEASE suggest solutions
THANKS IN ADVANCE

Discussion is locked
You are posting a reply to: rand function not random
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: rand function not random
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Collapse -
Well discussed.
by R. Proffitt Forum moderator / May 27, 2009 11:44 PM PDT

You didn't reveal the code or what SEED you used for the random function.

Why would a compiler highlight a function? Maybe a code editor would but a compiler should never highlight.
Bob

Collapse -
I remember this
by MarkFlax Forum moderator / May 28, 2009 7:54 AM PDT

from the old Zilog Z80 BASIC language days, (ZX Spectrum). Remember those? Nothing like C or C+, etc, but the problem is the same.

You're right, Rand is not random because the processor has to calculate it from a fixed set of instructions. So it always starts off from the same place. In fact it is difficult to create a truly random number for that reason.

From those days I remember that I had to reset the seed using RANDOMIZE, then an expression like INT(RND40)+1, to give a random whole number between 1 and 40.

Not sure how that helps you though.

Mark

Popular Forums
icon
Computer Newbies 10,686 discussions
icon
Computer Help 54,365 discussions
icon
Laptops 21,181 discussions
icon
Networking & Wireless 16,313 discussions
icon
Phones 17,137 discussions
icon
Security 31,287 discussions
icon
TVs & Home Theaters 22,101 discussions
icon
Windows 7 8,164 discussions
icon
Windows 10 2,657 discussions

CNET FORUMS TOP DISCUSSION

Help, my PC with Windows 10 won't shut down properly

Since upgrading to Windows 10 my computer won't shut down properly. I use the menu button shutdown and the screen goes blank, but the system does not fully shut down. The only way to get it to shut down is to hold the physical power button down till it shuts down. Any suggestions?