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

random number generator help

P: 3
im am very new to programming and i dont understand these questions that were in the exercise part in my book :

1. Write a single statement that prints a number at random from the set:
3,5,7,9,11

2. Write a statement that assigns random integers to the variable n in the range of 1 to 100.

can someone help me in regrads to what is the difference between assigning and just copying? and how would i answer these. thanks!
Nov 5 '06 #1
Share this Question
Share on Google+
1 Reply


sicarie
Expert Mod 2.5K+
P: 4,677
In my experience, the key to this would be the modulus function - '%'.

For the first part, I would suggest reading the numbers into an array, and then getting a number from the random number generator, and then do something like

Expand|Select|Wrap|Line Numbers
  1. index = rand() % 5;
  2.  
and then you can print array[index].

And your second one is as easy as doing a modulus of 100. The modulus is the remainder of a division, and because of that property, is kept in the range of 0->n-1 (which means you might want to add one to the result of the modulus, I think...)
Nov 5 '06 #2

Post your reply

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