473,387 Members | 1,619 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,387 software developers and data experts.

Random number generator in c# - again

Hello :-)

I've got lame begginer question about generating different random
numbers in C# ;-)

First, some code:
public int test(int lb)
{
int count = 0;
Random randSeed = new Random();

for(int i=0;i<lb;i++)
{
double test = randSeed.NextDouble();
if(test>0.49) {
count++;
}
}
return count;
}

The code _should_ give me all positive scores of 'lb' throws of coin.
And it does. But when called couple of times in the "same" time, like:

test(23); test(12); test(45);

it generates the same scores..

How do i hop over it in C# ? ;-)
--
Best regards.. or not..
Piotr 'Zenobius' Baranowski
Nov 17 '05 #1
5 15831
Hi Piort,
I am not sure what you mean by "hop over it" but you can try to make it
more random by adding a seed to the constructor of the random class, or
create the random class outside of your test method and pass it in so that
all counts use the same random object i.e.

Random r = new Random((int)DateTime.Now.Ticks);

test(23, r);
test(15, r);
test(45, r);

Or if you want truley random numbers you might want to try using
System.Security.Cryptography.RandomNumberGenerator which will give you better
results, but I believe will be slower.

Hope that helps
Mark R Dawson

"Piotr 'Zenobius' Baranowski" wrote:
Hello :-)

I've got lame begginer question about generating different random
numbers in C# ;-)

First, some code:
public int test(int lb)
{
int count = 0;
Random randSeed = new Random();

for(int i=0;i<lb;i++)
{
double test = randSeed.NextDouble();
if(test>0.49) {
count++;
}
}
return count;
}

The code _should_ give me all positive scores of 'lb' throws of coin.
And it does. But when called couple of times in the "same" time, like:

test(23); test(12); test(45);

it generates the same scores..

How do i hop over it in C# ? ;-)
--
Best regards.. or not..
Piotr 'Zenobius' Baranowski

Nov 17 '05 #2
Hi

System.random namespace

Random r = new Random()

I think it also has facility to generate random numbers in integer ,
double etc etc

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/

Nov 17 '05 #3
Piotr 'Zenobius' Baranowski <ze******@zeno.pl> wrote:

<snip>
The code _should_ give me all positive scores of 'lb' throws of coin.
And it does. But when called couple of times in the "same" time, like:

test(23); test(12); test(45);

it generates the same scores..

How do i hop over it in C# ? ;-)


Just create a single Random and use it repeatedly. By default, the
current time is used for the initial seed for a new Random instance. If
you create two Randoms at the same time (as you are doing, effectively)
you'll get the same results multiple times.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Jon Skeet [C# MVP] wrote:
Just create a single Random and use it repeatedly. By default, the
current time is used for the initial seed for a new Random instance. If
you create two Randoms at the same time (as you are doing, effectively)
you'll get the same results multiple times.


This works the same, 2-4 scores are the same ;-)

I just wanted to create function, that simulates real k2 rolls.. now i'm
using Random.Next(lb+1), and it works fine.
--
Best regards.. or not..
Piotr 'Zenobius' Baranowski
Nov 17 '05 #5
Piotr 'Zenobius' Baranowski <ze******@zeno.pl> wrote:
Just create a single Random and use it repeatedly. By default, the
current time is used for the initial seed for a new Random instance. If
you create two Randoms at the same time (as you are doing, effectively)
you'll get the same results multiple times.
This works the same, 2-4 scores are the same ;-)


In that case, you're either not actually using a single random, or
you're just being unlucky in seeing the same values.
I just wanted to create function, that simulates real k2 rolls.. now i'm
using Random.Next(lb+1), and it works fine.


Righto.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
4
by: Greg Strong | last post by:
Hello All, Is it possible to create multiple random numbers in a query where there are numerous records? I've created a custom function. When I use it in a query it creates the same random...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
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...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
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,...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.