Doubts regarding random numbers generation

This is a discussion on Doubts regarding random numbers generation within the C Programming forums, part of the General Programming Boards category; At first my new year wishes to all members. My doubts are 1. How can i generate 10 random numbers ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    8

    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

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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()

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by girish1026 View Post
    My doubts are
    im confused by your use of "doubts". do you mean something like "constraint" or "restriction"/"requirement"?

    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).

    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?!
    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.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    Quote Originally Posted by nadroj View Post
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by girish1026 View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    Quote Originally Posted by iMalc View Post
    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.
    Last edited by grumpy; 12-31-2008 at 03:56 AM.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I have a doubt regarding the use of doubt, is it okay?

    Oh snap.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    <LOL>

    Thanks, Zacks7!

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by grumpy View Post
    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

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nadroj View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Generatin Random Numbers
    By dantu1985 in forum C Programming
    Replies: 15
    Last Post: 08-13-2007, 01:21 AM
  3. Random numbers
    By Mavix in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 09:01 AM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21