random number generator

This is a discussion on random number generator within the C Programming forums, part of the General Programming Boards category; I am trying to implement a random number generator I am not using visual studies but the complier is still ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    43

    random number generator

    I am trying to implement a random number generator

    I am not using visual studies but the complier is still a c complier and i will explain any of the code that is not the standard "norm" in visual studies

    so here is what i am having an issue with, is that every time i run the program at intial start up i get the same random four numbers

    All i trying to do is write a function that will give me random numbers on a hex scale from 0x00 to 0xFF

    so here is the code


    Code:
    #include "stdlib.h"
    #include "time.h"
    #include "string.h"
    #include "stdio.h"
    
    
    int num1, num2, num3, num4;
    
    
    int gen_rand(void);
    
    int randomNumber;
    
    void main(void)
    {
          	num1=gen_rand( );
    	num2=gen_rand( );
    	num3=gen_rand( );
    	num4=gen_rand( ); 
    		
    		
    	LCD_Position(0,0);
    	LCD_PrHexByte(num1);
    		
    	LCD_Position(0,10);
    	LCD_PrHexByte(num2);
    		
    	LCD_Position(1,0);
    	LCD_PrHexByte(num3);
    	
    	LCD_Position(1,10);
    	LCD_PrHexByte(num3);
    
    
    }
    
    int gen_rand(void)
    {
    	
    		int n;
    		n=rand( );
    	
    		return (n);
    	
    }
    the lcd positon commands are just to put the random number generator in a spacific spot on the lcd and the prCHexByte is to just put the random number into a hex value so that i can use it in my project

    and every time i start the program i get A6 E7 94 94
    which is random, but not random enough that everyitme i start the program i get the same results


    inside my my main i put a while(1) loop to just sit there and spit out random numbers but i want to program a way that everytime i call the random function i get a different value

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yes the random function will always generate the same sequence of numbers. That's done on purpose to make program testing consistent/easier. To make it change you'll have to seed it using something like the system time. That's a standard technique because it's guaranteed to be different each time. Look up random seed...

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      srand(time(NULL));  // Seed the RNG with the current time
      printf("%d\n", get_rand());
      return 0;
    }
    
    int get_rand()
    {
      return rand() % 0x100;  // Random number between 0x0 and 0xFF inclusive
    }
    I didn't test the code, but it should work. Someone will undoubtedly complain about the use of the modulus operator for reducing the number set, but it's good enough for demonstration purposes.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by nonoob View Post
    That's done on purpose to make program testing consistent/easier.
    No, the reason is that you can't generate truly random numbers.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Fronty ... you're right... but nonoob (like many of us) knows how to capitalize on it.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fronty View Post
    No, the reason is that you can't generate truly random numbers.
    Incorrect. Random number generators in C, are built to repeat their sequence of numbers, if they are given the same seed (or no seed). That is done to facilitate testing of both software and hardware.

    Of course, a deterministic system can't generate a truly random number without some kind of non-deterministic device to help it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 06:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 04:10 PM

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