Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Random Numbers

    How do you generate random numbers within certain limits? Such as a random number between 0.2 and 4.3?

    And, if this is possible how do you call it within a function. Such to generate 100 random numbers between the limits then add them together.

    Thanks.

  2. #2

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  4. #4
    Hello,

    Creating random numbers using srand() and rand() is not difficult. According to Thantos and Quzah, a forum search or FAQ check may solve your problem.

    Though on the other hand I too was pondering how to generate random floating-point numbers. Doing it the simple way will always fail if you use the modulus operator (%), hence that rand() returns an integer-type variable only. Though this got me thinking.

    I had to keep in mind that if we want to produce numbers in a specific range we could use the modulus, or remainder, operator divides number1 by number2, and returns only the remainder as result. It's not the best way to generate a range but it's the simplest. If we use "rand() % n" we generate a number from 0 to n-1. By adding an offset to the result we can produce a range that is not zero based. Though this only applies to whole-numbers / integers.

    Though I wanted a random number between 0.3 and 1.1. Most of all decimal points can be reversed by multiplication. Let's do a loop around. Here is the logic in this:
    Code:
    multiply your fraction to a whole number
    	get random number from whole number
    divide your random integer by the amount you multiplied
    This makes perfect sense. The larger your multiplication factor, the more you will get a "random" choice. Of course casting applies too. Cast rand() as a double for float, etc... I just sought a challenge, and did it a way I could always remember.


    - Stack Overflow
    Last edited by Stack Overflow; 11-03-2004 at 01:51 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Stack Overflow
    Though I wanted a random number between 0.3 and 1.1.
    Code:
    #include <stdlib.h>
    
    double foo(double lo, double hi)
    {
       return rand() / (RAND_MAX + 1.0) * (hi - lo) + lo;
    }
    
    /* ... */
    
       double value = foo(0.3, 1.1);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM