Hi,
I'm trying to generate 3 random numbers inside a while loop.
The problem I encountered is that I always get the same 3 numbers even after I reseed the number using srand((unsigned)time(NULL));
while (J < 3)
{
srand((unsigned)time(NULL)); //Seed the random number generator.
int Rand1 = rand() % 100 + 1;
srand((unsigned)time(NULL)); //Seed the random number generator.
int Rand2 = rand()% 10 + 1;
srand((unsigned)time(NULL)); //Seed the random number generator.
int Rand3 = (rand() % 34 + 4);
J++;
}
If I put a pause at the end of the while loop then it works, as the time changes,
but I need it to be more frequent.
any help would be greatly appreciated.
Regards,
BB
4 13105
you reseed each time you call rand.....
since the call to time returnsd a time in seconds, the processor would have to be pretty slow for this to work.
As rand() returns a pseaudo-random STREAM of numbers (which I might use for basic applications, but wouldn't recommend using for important (especiall security critical) applications). This means you don't need to reseed it each time you use it (and reseeding will set the seed to the same number again in this case, returning the same number)....
Try using
[/code]
while (J < 3)
{
srand((unsigned)time(NULL)); //Seed the random number generator.
int Rand1 = rand() % 100 + 1;
int Rand2 = rand()% 10 + 1;
int Rand3 = (rand() % 34 + 4);
J++;
}
[/code]
instead....
if you are using a Pentium PC you can use the Time Stamp Counter to seed the random number generator
http://improv.sapp.org/doc/class/SigTimer/
code using the gcc compiler -
// seed random number generator with Time Stamp Counter
-
unsigned long long int clockCount;
-
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (clockCount));
-
srand((unsigned)clockCount);
-
int Rand1 = rand() % 100 + 1;
-
you reseed each time you call rand.....
since the call to time returnsd a time in seconds, the processor would have to be pretty slow for this to work.
As rand() returns a pseaudo-random STREAM of numbers (which I might use for basic applications, but wouldn't recommend using for important (especiall security critical) applications). This means you don't need to reseed it each time you use it (and reseeding will set the seed to the same number again in this case, returning the same number)....
Try using -
while (J < 3)
-
{
-
srand((unsigned)time(NULL)); //Seed the random number generator.
-
int Rand1 = rand() % 100 + 1;
-
int Rand2 = rand()% 10 + 1;
-
int Rand3 = (rand() % 34 + 4);
-
J++;
-
}
-
instead....
This code will still be inadequate, as each execution of the loop will produce the same 3 numbers every time. You should use srand ONCE in your program, at the very beginning of your main program, and then NEVER AGAIN. This will ensure that there is no resetting of the generator, and you will not get repeats in this manner (You may still get repeats of numbers because of the randomness).
This code will still be inadequate, as each execution of the loop will produce the same 3 numbers every time. You should use srand ONCE in your program, at the very beginning of your main program, and then NEVER AGAIN. This will ensure that there is no resetting of the generator, and you will not get repeats in this manner (You may still get repeats of numbers because of the randomness).
Thank you Ganon11, this did it...
I've looked everywhere for this...
Now I can run my loop without any pauses and it works great.
thanks a lot..
BB
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paul C-T |
last post by:
Hi,
Is there a way to repeat a set of code until a certain if statement is
satisfied. If it is then to exit the loop and if not repeat the code?
Say I want to write a card game application in...
|
by: Aaron |
last post by:
I need some help writing this function
the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.
string randGen(int maxRange)
...
|
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...
|
by: Ben Pfaff |
last post by:
One issue that comes up fairly often around here is the poor
quality of the pseudo-random number generators supplied with many
C implementations. As a result, we have to recommend things like...
|
by: quickcur |
last post by:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number...
|
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...
|
by: Mike Langworthy |
last post by:
I am having a problem Breaking out of the while loop, it never terminates.
any suggestions
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{...
|
by: Anil Gupte |
last post by:
I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:
public string GetRand(int count)
{
string number = "";
for (int i=0;...
|
by: sonyb |
last post by:
In this program , I informed the user to enter 3 random numbers .. Each one of these numbers were taken and entered into an function (2x + 5) and after the 3rd number was inserted into this...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |