collapse collapse

* Who's Online

  • Dot Guests: 80
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11163
  • stats Total Topics: 1699
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 248

Author Topic: Random Number Generator  (Read 2449 times)

0 Members and 1 Guest are viewing this topic.

Offline mager1794

  • Jr. Member
  • **
  • Posts: 91
  • Reputation 293
    • View Profile
    • My Game Challenge
Random Number Generator
« on: March 15, 2014, 10:59:47 am »
I created a conceptual Random Number Generator by establishing a table filled with randomly pressed numbers reaching 8 digits. With the highest number being 59999999.

I don't exactly have the time to create a perfect random number generator so its not the most randomness.

At its highest capacity its pretty much a 1/n chance to be picked where n is the size of my number array.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    public static class NumberGenerator
    {
        //Type out random numbers
        //Max Number 59999999
        //Min Number 0
        static int currentPosition = 0;
        static int[] number = {0, 38491028, 59293982, 19297329, 42738293, 39927290, 59281023, 18326283, 29127492, 5999999, 50192839, 19382739,29372382,19283749 , 49282328, 32817238, 19237482, 59182839,12938292,29385919,39581828};


        public static int Next()
        {
            currentPosition++;
            if (currentPosition >= number.Length)
                currentPosition = 0;

            return number[currentPosition];
        }

        public static int Next(int min,int max)
        {
            return (Next()% (max - (min - 1))) + min;
        }

        public static void Seed()
        {
            Seed(DateTimeToInteger(DateTime.Now));
        }

        public static void Seed(int i)
        {
            currentPosition = (i % number.Length);
        }

        public static int DateTimeToInteger(DateTime time)
        {

            Console.WriteLine(number.Length.ToString());

            string s = "";

            s += time.Year.ToString();
            s += time.Day.ToString();
            s += time.Minute.ToString();


            return int.Parse(s);
        }
    }
}
http://mygamechallenge.wordpress.com/2010/07/23/personal-game-challenge/ - My Personal Challenge To Finish My First C++ Game

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: Random Number Generator
« Reply #1 on: March 15, 2014, 05:45:26 pm »
I propose using a unix timestamp for the datetime number:

Code: [Select]
public static int DateTimeToInteger(DateTime time)
{
TimeSpan span = (this.date - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
    return (int)span.TotalSeconds;
}

Here is a whole class that deals with Unix timestamp in C#:

Code: [Select]
class Unixtime
{
    private DateTime date;
    public Unixtime()
    {
        this.date = DateTime.Now;
    }
    public Unixtime(string date)
    {
        this.date = DateTime.Parse(date);
    }
    public Unixtime(double timestamp)
    {
        DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        DateTime newDateTime = converted.AddSeconds(timestamp);
        this.date = newDateTime.ToLocalTime();
    }
    public DateTime toDateTime()
    {
        return this.date;
    }
    public int toUnixTimestamp()
    {
        TimeSpan span = (this.date - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
        return (int)span.TotalSeconds;
    }
}

I do want to make a comment - this idea is fine for a game or simple application but don't use it in any sort of cryptography :). The bad guy will know all possible values.
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline mager1794

  • Jr. Member
  • **
  • Posts: 91
  • Reputation 293
    • View Profile
    • My Game Challenge
Re: Random Number Generator
« Reply #2 on: March 15, 2014, 11:37:27 pm »
True, but isn't this the basis for most modern random number generators? I've seen quite a few random number algorithms and the most proficient is table based.  Granted its usually a much larger table.

http://mygamechallenge.wordpress.com/2010/07/23/personal-game-challenge/ - My Personal Challenge To Finish My First C++ Game

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: Random Number Generator
« Reply #3 on: March 16, 2014, 01:09:07 am »
True, but isn't this the basis for most modern random number generators? I've seen quite a few random number algorithms and the most proficient is table based.  Granted its usually a much larger table.



I always thought they were based on an algorithm:

http://en.wikipedia.org/wiki/Mersenne_twister
http://computer.howstuffworks.com/question697.htm
http://stackoverflow.com/questions/6275593/how-to-write-you-own-random-number-algorithm
http://mathoverflow.net/questions/29494/pseudo-random-number-generation-algorithms

But I do suppose they need a value or set of values to start with. Just like how MD5 has a lookup table - http://en.wikipedia.org/wiki/MD5
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

 

Donate


* Search


* Recent Posts

Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]


Re: Client-Server Messaging by Nathan
[October 25, 2015, 05:48:57 pm]