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

I have a problem with random numbers

Savage
1,764 Expert 1GB
Of 7 random numbers it's happening that some of these numbers are the same.
I tryed to use do-while loop to fix this, but problem still consist.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. input:
  2.     enum bool {FALSE,TRUE};
  3.     unsigned int a[BR_BROJ],b[BR_BROJ],p[BR_BROJ],q[BR_BROJ];
  4.     int x,y,k,i,j,L,kk,xi,yi;
  5.     int e,w;
  6.     int br,m=0;
  7.  
  8.     for(i=0;i<BR_BROJ;i++) a[i]=b[i]=p[i]=q[i]=0;
  9.     textbackground(7);
  10.     textcolor(4);
  11.     clrscr();
  12.     do{
  13.         randomize();
  14.         for(i=0;i<BR_BROJ;i++) a[i]=random(39)+1;
  15.  
  16.     }while((a[1]==a[2]||a[1]==a[3]||a[1]==a[4]||a[1]==a[5]||a[1]==a[6]||a[1]==a[7])&&(a[2]==a[3]||a[2]==a[4]||a[2]==a[5]||a[2]==a[6]||a[2]==a[7])&&(a[3]==a[4]||a[3]==a[5]||a[3]==a[6]||a[3]==a[7])&&(a[4]==a[5]||a[4]==a[6]||a[4]==a[7])&&(a[5]==a[6]||a[5]==a[7])&&(a[6]==a[7]));
Please help.
Feb 28 '07 #1
5 1293
DeMan
1,806 1GB
I think the problem may ber not so much that two numbers are the same, but rather that the while loop should only have or (||) and not and (&&)
Certainly some more parentheses would simplify the logic (and that seems a very tedious way to check....

It may be easier to :

1 loop through as you are now
2.1 choose a random number
2.2 check whether it's in the array here and regenerate if necessary
3 continue on
Feb 28 '07 #2
Savage
1,764 Expert 1GB
I think the problem may ber not so much that two numbers are the same, but rather that the while loop should only have or (||) and not and (&&)
Certainly some more parentheses would simplify the logic (and that seems a very tedious way to check....

It may be easier to :

1 loop through as you are now
2.1 choose a random number
2.2 check whether it's in the array here and regenerate if necessary
3 continue on
I tried that before and it looks like it does not help

Thanks for the help
Feb 28 '07 #3
Ganon11
3,652 Expert 2GB
I tried that before and it looks like it does not help

Thanks for the help
Which did you try? His first explanation, or his alternate decision?
Feb 28 '07 #4
Banfa
9,065 Expert Mod 8TB
Your while loop is completely failing to test a[0], however it is testing a[7] but you say only generate 7 numbers clearing starting from index 0 in the generating loop which suggests that a[7] is an out of bounds array access.
Mar 1 '07 #5
Savage
1,764 Expert 1GB
Your while loop is completely failing to test a[0], however it is testing a[7] but you say only generate 7 numbers clearing starting from index 0 in the generating loop which suggests that a[7] is an out of bounds array access.
Uhm, I see thanks for the help.

Savage
Mar 1 '07 #6

Sign in to post your reply or Sign up for a free account.

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...
34
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? ...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
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...
23
by: Alvin | last post by:
Well, I'm developing a Tetris game in SDL, but when it comes to deciding the next block, I'm stuck. It's random, but when I try something like seeding the randomizer with the time, it won't update...
9
by: Karlo Lozovina | last post by:
Here is it: --- class Human: def __init__(self, eye_one, eye_two): self.eye_one = eye_one self.eye_two = eye_two class Population: def __init__(self):
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
3
by: Ethan Strauss | last post by:
Hi, There have been quite a few discussions of Random not giving random numbers and how to fix that by feeding in a new seed each time, waiting enough time, or calling the Next() method of the...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
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 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.