By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,186 Members | 1,530 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,186 IT Pros & Developers. It's quick & easy.

Generating random numbers

P: 16

This is my second post till now. Thanks for all the advice given to me for the first post.

Now I'm having problem with generating random numbers. I know if we want to generate a random number, we can use:

int tempNUM = rand();

But, once I execute the program, the generated number is always similar, and I found this from the internet. It says need to add this line to make the numbers generated differently:


When I add that line, my result is like hell.. Could someone please kindly enlighten me. Thank you
Oct 1 '07 #1
Share this Question
Share on Google+
12 Replies

P: 65
What kind of hell is the result you get?
Oct 1 '07 #2

Expert 100+
P: 464
If you want to limit the results from the random number you can use the modules operator.

Number = rand() % 200// generates a number from 0-199
Oct 1 '07 #3

Expert Mod 2.5K+
P: 4,677
Yeah, you're not actually generating a "random number" per se, but more a pseudo-random number. There are algorithms for getting a very dispersed range, some positive, some negative, etc.... One cool thing is that if you set the seed with the same number, you can come up with the same 'random numbers' each time to duplicate experiments.

You probably are getting a 'random number.' Is there a specific range that you need it be between? (Ah, see Studlyami's post above for how to get it to be in a range)
Oct 1 '07 #4

P: 2

I couldn't find any new & interesting topic of my C language project so I want a topic only .....

Give me suggestions for the topic of my C language project ...

NOTE :- I have knowledge of C language only so give only those topic in which C language is used......

Send ur suggestion Quickly my friends.....
Oct 1 '07 #5

Expert Mod 2.5K+
P: 4,677
Jayant Vikram-

Posting your own question in a thread is called thread hijacking, and not only is it extremely rude, but it is also against the Posting Guidelines for theScripts. Please review the Guidelines and ask your own question in your own thread.
Oct 1 '07 #6

P: 16
Yes I understand that the range can be obtained as follows to get numbers between 1~3:

int randNum;
randNum = (rand()%3)+1;

But the problem I faced right now is, let's say I've add on a cout as follows:

cout << "Number generated: " << rand() << endl;

Let's give an example, when I run the program for the first time, the result is:

Number generated: 0.41

I close the command prompt and run the program the second time, it still shows me the same result, which is the problem I'm facing now. I want the numbers to differ each time I run the program. Any advice would be appreciated. Thanks
Oct 1 '07 #7

Expert Mod 2.5K+
P: 4,677
This didn't work? Are you getting errors? What are you getting?
Oct 1 '07 #8

P: 16
No, it's not a compilation error. Well, before I add on that line, the program can run properly. But after I add on that line, the result shown on the command prompt is like an endless loop whereby the numbers are jumping non-stop.

Since after adding that line will make the numbers generated randomly, but I'm still setting a proper range for the numbers generated in order to get the range that I want. I can't understand why this line can suddenly make my programming not working. My programming is not really completed yet so I can't really show it here though...
Oct 1 '07 #9

Expert Mod 2.5K+
P: 4,677
If you can't post the snippet of code you're having trouble with, it's going to be a bit difficult to help troubleshoot it any further.
Oct 1 '07 #10

P: 16
Or am I doing the wrong thing? without that line, rand() itself will generate numbers between 0~1 right? Because:

tempNUM = rand();
cout << "Random number generated is :" << tempNUM << endl;

and all I see is the same (for e.g) 0.41.

What will happen if I add srand((unsigned)time(0)), will the numbers being generated more than 1?
Oct 1 '07 #11

Expert Mod 2.5K+
P: 4,677
I'm not really sure what your implementation is - I keep getting (mostly) large values, so I'm going to point you to the documentation of rand(), and wish you the best. If you want to post code, we could probably help more, but I'm not sure what exactly you're doing in your program.
Oct 1 '07 #12

P: 16
I'm actually solving a TSP problem whereby I need to write a function to search the possible randomly. So I need to generate random numbers to help me choose which paths should I take in case there are more than 1 path. My current program is very very long already, so I don't know which snippet should I post it here. But anyway, thanks a lot for your advice. I think I've made some mistake in vectors due to random numbers. Because I'm using a lot of

vector1[randNum] = ......

So I think I'll take a look at the documentation you have given. Thanks a lot
Oct 1 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.