473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Random Numbers? What to use?

What would be the best way to generate random numbers for games. At the
moment I use the following to seed the generator. Can anyone think of another
way

Random mRand = new Random(unchecked((int)DateTime.Now.Ticks));
Nov 16 '05 #1
7 1458
You can also use System.Security.Cryptography.RNGCryptoServiceProvi der with
GetBytes() and GetNonZeroBytes() methods. It's a bit slower than Random, but
every byte is computed with a seed dependent on lots of environment
conditions so it is almost random (that is why it is used in cryptographic
functions). Random bytes are linear beginning with the value computed with
constructor seed.
Hope this helps.
Wojtek

What would be the best way to generate random numbers for games. At the
moment I use the following to seed the generator. Can anyone think of another way

Random mRand = new Random(unchecked((int)DateTime.Now.Ticks));

Nov 16 '05 #2
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in
news:FC**********************************@microsof t.com...
What would be the best way to generate random numbers for games. At the
moment I use the following to seed the generator. Can anyone think of
another
way

Random mRand = new Random(unchecked((int)DateTime.Now.Ticks));


If you don't need cryptographic quality, this is usually sufficient:

Random mRand = new Random();

According to the docs, it initializes the Random object with a
time-dependent value.

Niki
Nov 16 '05 #3
> According to the docs, it initializes the Random object with a
time-dependent value.


This is true. The Random class is internally instantiated using the
DateTime.Now.Ticks value. However, if you're up to generating large amounts
of random numbers, I would probably look into reusing a Random class
instance (or having an instance stored somewhere, then invoked), instead of
instantiating each time you need a random number (as the ticks property
isn't 100% realtime).

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #4
> This is true. The Random class is internally instantiated using the
DateTime.Now.Ticks value. However, if you're up to generating large
amounts
of random numbers, I would probably look into reusing a Random class
instance (or having an instance stored somewhere, then invoked), instead
of
instantiating each time you need a random number (as the ticks property
isn't 100% realtime).


Never mind that, if you re-instantiate the standard Random class not only
you pay a certain performance quality, but also, none of the statistical
properties of the generator are guaranteed.
The correct way of using any random number generator is to initialise it and
then use its "Next()" method to extract subsequent random numbers.
--
WildHeart'2k4
Nov 16 '05 #5
> Never mind that, if you re-instantiate the standard Random class not only
you pay a certain performance quality, but also, none of the statistical
properties of the generator are guaranteed.
The correct way of using any random number generator is to initialise it and then use its "Next()" method to extract subsequent random numbers.


Naturally, but he asked and I wanted to let him know of this behaviour. Any
problems with that? ;-)

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #6
"Anders Borum" <a@b.dk> wrote in
news:eh**************@tk2msftngp13.phx.gbl...
Never mind that, if you re-instantiate the standard Random class not only
you pay a certain performance quality, but also, none of the statistical
properties of the generator are guaranteed.
The correct way of using any random number generator is to initialise it

and
then use its "Next()" method to extract subsequent random numbers.


Naturally, but he asked and I wanted to let him know of this behaviour.
Any
problems with that? ;-)


I somehow assumed the OP already knew that. He only asked how to seed the
RNG. I guessed someone who knew what a "seed" is should know how a PRNG
works (basically).

Niki

Nov 16 '05 #7
Thanks for the help, and Yes I do know about the Next call, I was just
asking about the prefered method of seeding as I will need fast performance
and complete random numbers. I have seen systems in the past that when
generating random numbers depending on how fast they are called can produce
the same series of numbers.

"Niki Estner" <ni*********@cube.net> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
"Anders Borum" <a@b.dk> wrote in
news:eh**************@tk2msftngp13.phx.gbl...
Never mind that, if you re-instantiate the standard Random class not
only
you pay a certain performance quality, but also, none of the statistical
properties of the generator are guaranteed.
The correct way of using any random number generator is to initialise it

and
then use its "Next()" method to extract subsequent random numbers.


Naturally, but he asked and I wanted to let him know of this behaviour.
Any
problems with that? ;-)


I somehow assumed the OP already knew that. He only asked how to seed the
RNG. I guessed someone who knew what a "seed" is should know how a PRNG
works (basically).

Niki

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
5
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.