Results 1 to 4 of 4

Thread: Generating random number using library functions

  1. #1
    Join Date
    Jan 2010
    Posts
    63

    Generating random number using library functions

    please someone can help me in writing a random number generator function. generates number between 1 to 10000 with explanation.
    what is the existence of srand function and time function and how it makes enable to generate random numbers. How can I perform seeding of numbers in the process of generating random numbers.I wanted to know about the Random functions with time seed.
    Last edited by Laquan; 21-01-2010 at 08:34 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Generating random number with time speed

    I think, if I will go for random number generation function it can take a large space in the part of codes in the program and program code would be so large. so we need to use time as a seed.
    I am going to expand a lot of my strategy but try to compile it and use your own logic to edit it how much you can.


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>


    int main()

    {

    int rdom, sd, time;
    sd = (int) time(NULL);
    rdom = rand(sd);
    printf("random number = %d\n", rdom);
    return (0);

    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Generating random number using C strategy

    You can generate random numbers between a specific range and it will be happen through the rand function that is stored in the stdlib header file.
    However it is little bit difficult to construct in C++ .

    Code for generating random number:


    #include <iostream.h>
    #include <stdlib.h>

    int generate(int,int);

    main()

    {

    srand(time(0));

    int Minvalue, Maxvalue;

    int num=generate(Minvalue,Maxvalue);

    Printf ("The generated number is : ",num);

    getch();

    }

    // Function coding below.............................

    int generate(int Minvalue,int Maxvalue)

    {

    int num=(Minvalue+rand())%Maxvalue;

    return num;

    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Generating random number using srand function in C++

    If you will produce random numbers between a specified range.It's not a best way but you can say it is very simple to construct.
    I am going to construct a program which will generate 20 random numbers between 1 to 10. I will make it using both functions rand and srand.


    #include <cstdlib>
    #include <ctime>
    #include <iostream>

    using namespace std;

    int main()

    {

    srand((unsigned)time(0));

    int rdom_int;

    for(int indx=0; indx<20; indx++)

    {

    rdom_int = (rand()%10)+1;

    cout << rdom_int << endl;

    }

    }

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Replies: 2
    Last Post: 09-10-2011, 10:15 PM
  3. How to use "Math.random()" to generate a random number in JavaScript?
    By Silent~Kid in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 05:06 AM
  4. Problem in random number
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 01:30 PM
  5. Compile time error while generating Random Number in C++
    By Suzane in forum Software Development
    Replies: 3
    Last Post: 14-04-2009, 08:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,788,191.26091 seconds with 16 queries