# Doubts regarding random numbers generation

Printable View

• 12-30-2008
girish1026
Doubts regarding random numbers generation
At first my new year wishes to all members.

My doubts are

1. How can i generate 10 random numbers from 1 to 10, But here the condition is the generated random numbers should be odds only and not evens or vice versa.

2. The srand() function uses a different seed value on every run, causing a different set of random values every run. According to this, the srand() function gets one different vlaue for each run. Can we can't get more than one values for srand() function for each run of program.

Thanks
• 12-30-2008
zacs7
1. use rand() % 10 + 1 if it's even (result % 2 == 0) then add 1 to make it odd be careful with 10 though -- 10 is even so you can ignore that anyway and generate from 1 to 9.
2. What?!? You seed the random number generator with srand()
• 12-30-2008
nadroj
Quote:

Originally Posted by girish1026
My doubts are

im confused by your use of "doubts". do you mean something like "constraint" or "restriction"/"requirement"?

Quote:

1. How can i generate 10 random numbers from 1 to 10, But here the condition is the generated random numbers should be odds only and not evens or vice versa.
so you want even numbers from 1-10? simply generate numbers from 1-5 and multiply each by 2 (resulting in 2,4,6,8,10).

Quote:

2. The srand() function uses a different seed value on every run, causing a different set of random values every run.
otherwise it wouldnt be very random would it?!
Quote:

According to this, the srand() function gets one different vlaue for each run. Can we can't get more than one values for srand() function for each run of program.
your wording is confusing. what exactly are you asking? if you dont provide a seed, you should get the same sequence if thats what your looking for. if your wanting to provide two seeds there isnt really a point, the first one should make it random enough. most often people will use the current time as the seed.
• 12-31-2008
grumpy
Quote:

Originally Posted by nadroj
so you want even numbers from 1-10? simply generate numbers from 1-5 and multiply each by 2 (resulting in 2,4,6,8,10).

And subtract 1 to get the odds ;)
• 12-31-2008
iMalc
Quote:

Originally Posted by girish1026
My doubts are

Just so you know, you've been taught wrong. The word you should have used is "question", not "doubt", and a question must end in a question mark (a "?").
You can be pretty sure that the word "doubt" is basically never the right word to use if English is your second language.
• 12-31-2008
grumpy
Quote:

Originally Posted by iMalc
You can be pretty sure that the word "doubt" is basically never the right word to use if English is your second language.

I sort of doubt that ;)

Just to clarify for girish1026, however, the word "doubt" is a verb for "being uncertain", but it is always used in contexts where there is some level of distrust. The word "question" is used to raise a concern for discussion, when seeking a reply or explanation, in a more neutral manner.
• 12-31-2008
zacs7
I have a doubt regarding the use of doubt, is it okay?

Oh snap.
• 12-31-2008
Adak
<LOL>

Thanks, Zacks7! :)
• 12-31-2008
nadroj
Quote:

Originally Posted by grumpy
And subtract 1 to get the odds ;)

hmm i dont know if you are serious or not! but if he/she was asking for even random numbers from 1-10, using random numbers from 1-5 and multiplying will give range of 2,4,6,8,10. im not sure what you mean by subtract 1 for the odds? unless of course I am misunderstanding the question! oh well :)
• 12-31-2008
Adak
Quote:

Originally Posted by nadroj
hmm i dont know if you are serious or not! but if he/she was asking for even random numbers from 1-10, using random numbers from 1-5 and multiplying will give range of 2,4,6,8,10. im not sure what you mean by subtract 1 for the odds? unless of course I am misunderstanding the question! oh well :)

Grumpy's a seriously smart guy, and he's not joking. (Note the forum name he uses)

You have even numbers when you multiply 1-5 by 2, in the range 1 through 10.

If you wanted odd numbers, instead of evens, you could do the same multiplication, and then subtract 1, to get the odd numbers, in the range of 1 through 10.